#PowerQuery – Calculate the ISO date from year and a week number

Just wanted to share a M function that I just created to calculate the date from a Year and a ISO week number.

The example data is the following

Now the function I created can be called with the Year and Week number as parameters to get the following result

The function has the following code and will return the date of the Monday of the week number.

(TheYear as number, TheWeek as number) as date =>
let
//test
//TheYear = 2018,
//TheWeek = 1,
//
offsetToISO = Date.AddDays(#date(TheYear,1,1),-4),
dayOfWeek = Date.DayOfWeek(offsetToISO),
offset = -dayOfWeek + (TheWeek * 7),
isoWeekDate = Date.AddDays(offsetToISO, offset)
in
isoWeekDate

Hope this can help you too.

Here is a link to an example pbix file – link

#PowerQuery – Control the expand columns so it includes new columns

Image a scenario where your column in your PowerQuery that contains a table with a set a columns that you know at some point will have more columns added.

In the case above I know that we at some point will add more columns in the merged ProductAttributes table.

How can we make this dynamic using PowerQuery

When we click the icon for expanding the table, we might just select this and move on

But notice the formula created in

It says

= Table.ExpandTableColumn(#”Merged Queries”, “ProductAttributes”, {“Brand”}, {“Brand”})

This means that even though we might add new columns to the ProductsAttributes table – it will still only be Brand that is expanded and only that column.

The bolded arguments is 2 lists that contains the Column names to expand and the new names of the columns – the last argument is optional so we can actually skip that if we want the original names – https://docs.microsoft.com/en-us/powerquery-m/table-expandtablecolumn

Now by changing the formula to this

= Table.ExpandTableColumn(#”Merged Queries”, “ProductAttributes”,List.RemoveItems(Table.ColumnNames(#”Merged Queries”[ProductAttributes]{0}), {“ProductKey”})
)

We can make the table dynamically expand when adding new columns in the table ProductAttributes

We get the new column included as well

The magic formula does this

Table.ColumnNames(#”Merged Queries”[ProductAttributes]{0})

Will return a list of column names from the step before
expansion (note I use the step name and column name) – and I use the {0} to extract the column names only form the first row – otherwise the formula will fail.

But as we cannot have the same column names twice (i.e. ProductKey needs to go away) so we need to use the List.RemoveItems functions

List.RemoveItems(Table.ColumnNames(#”Merged Queries”[ProductAttributes]{0}), {“ProductKey”})

Thereby removing the ProductKey Item in the list

And this means that when we get more columns in the table “ProductAttributes” table they will automatically be included in the expanded columns

Hope this can help you power queries even more dynamic.

Here is an example file –Β Link

Power Query On !

Adding keyboard shortcuts to your favourite buttons in #powerbi

If you find yourself clicking the same button in the Power BI Desktop you might find this little tip useful.

When you right click a button in the ribbon – you can add it to the Quick Access Toolbar.

This will add the button at the top (unless you have positioned it below the ribbon)

The button in the quick access toolbar can now be activated using a shortcut key combination.

When you press ALT the buttons will be labelled with numbers

So, pressing ALT + 7 will I my case open the Query Editor –

You can use the same technique in the Power Query Editor so by pressing ALT + 2 in my Query Editor will open the Advanced Editor

Hope this can help you too and do things even faster in Power BI with fewer clicks.

#powerbi Report to browse and watch sessions from #mbas 2019 using the #powerapp visual

Unfortunately I wasn’t able to participate in the Microsoft Business Application Summit this year – but luckily we can watch all the session via https://community.powerbi.com/t5/MBAS-Gallery/bd-p/MBAS_Gallery

But that would mean I had to leave Power BI Desktop in order to search and watch the videos – and the website also made it hard to see the distribution of sessions between categories.

So, I created this –

And to watch the video from within PowerBI I created a drill through page where I used the PowerApp visual to be able to show the

As none of the Microsoft standard visuals can play videos from within a report – I created a power App to show the video based on the selected video link.

If you want to embed this huge resource of learning material in your own tenant you can download the elements from here

The Desktop file – link

The Power App – link

If you are interested in learning how I scraped the web page for all the relevant data – check out these functions to extract data from pages using CSS query capabilities in the power query function Html.Table

Highly inspired by this blog post by Chris Webb – https://blog.crossjoin.co.uk/2018/08/30/power-bi-extract-urls-web-page/

How to get help for any function in #PowerQuery

One of the things you should know when working with PowerQuery is that you can get a list of all functions in M by adding a blank query and use the #shared expression to get all the functions.

This can be turned into a table by clicking the “Into Table” button on the ribbon and use the filter to find the function you want to learn about

Now when you click the cell “Value” for a given function the documentation of the function will appear below.

This is almost identical of what you can find online using the https://docs.microsoft.com/en-us/powerquery-m/power-query-m-function-reference – but you won’t have to leave the Query editor

But did you know

If you use the formula bar and

And complete it without parenthesis

You will get the documentation as well –

This is useful when you write your own Power Query formulas and need more info about the function that the intellisense gives you.

Power ON!

Move your pbix datamodel to Azure Analysis services – #powerbi

After the Azure Analysis Services web designer was discontinued per march 1 2019 – link – there is no official tool to do a move of a PBIX datamodel to Azure Analysis Service. But by using a few different tools we do have ways of doing it anyway.

Step 1 – DAX Studio

Open your PBIX file in the Power BI Desktop and then open the DAX Studio (link to download) and connect DAX studio to the PBI model

In the status bar you will see the local port of the Analysis model running on your machine

Step 2 SQL Management Studio

Connect to Analysis Services using the server address from Step 1

This will give you a connection to the model

Now right click the Database and choose to script the Database to a new Query Editor window (or the clipboard)

Step 3 Connect to Azure Analysis Services

Use the SQL Server Management Studio to connect to Azure Analysis Services

Select to run a New XMLA Query on the server

And paste the query created in Step 2 in the new Query window – You can specify a model name in the highlighted area

Run the Query – and after a few seconds you should get this result – that the query has completed successfully.

And after a refresh of the server object you should see the newly scripted data model

Step 4 Finish the move

Now depending on the data sources in your model you need to setup the necessary connections and gateway.

And use your favourite Analysis Services tool to modify model – for instance the Tabular editor ( link )

You will have to go through all your data sources and Tables to make sure the connections and M-Scripts are functioning.

Tip

Before you script your PBIX data model – turn off the Time intelligence in your current file – otherwise you could get a lot of extra date/time tables in your model

E-mail the selected record in #powerbi with #powerapps and #flow and include a CSV File with the records

One of my clients called me the other day and asked whether it was possible to export the selected order that was selected in the current report page – as she wanted to send the information to another user. I explained the export data feature from the visual action menu but she didn’t want to download a file and then locate that and then switch to Outlook and click new mail – type the correct the e-mail and attach the file – that was not very Power like – to much clicky clicky – because all the data was actually available when she had filtered the report for that particular record – the e-mail she wanted to mail the data to and off course the data she saw on the screen.

Hmm… Let’s see how we can use the PowerPlatform stack to solve this requirement.

A Power BI report to use a the demo

Based on the AdventureWorks database I build a report to select a Customer Key and filter it by a specific SalesOrderNumber

So, when the user has selected a sales order we should be able to send the information about the current sales order to either the customer or a alternate e-mail address.

PowerApps to works

Well we do have visual to integrate a PowerApp in our report and even though its in preview and there are some hickup’s we can make it work

But in order to insert it we do need to do this in a browser so let’s publish the report.

After I publish the report to the service, we can switch to edit mode and insert the PowerApp visual in the service.

This will introduce to a start screen with instructions on how to get started.

The important part is to include the fields from the datamodel you want to have access to in the PowerApp and this is done by adding the fields to the “PowerApps data” sink in the visuals pane.

As soon as you add the first field the PowerApp visual will change and let you select either to choose and app or create a new app

I added all these fields as I want to be able to access them in PowerApps

You have to use either Chrome or Edge browser to do this and keep calm – you will experience difficulties selecting the PowerApp visual with the mouse if you deselect it – try using the tab to rotate through the visual selection instead.

When all the fields is added we are ready to create a new app – well depending on the browser behaviour – so if it doesn’t work in Edge try in Chrome πŸ™‚ – so click new app and a new tab with PowerApps will appear

Notice that a Gallery control is inserted and its linked to “‘PowerBIIntegration’.Data” – which is the filtered records in PowerBI –

If I modify the Gallery Title field to show SalesOrderNumber instead – you can see the magic

As is selected in PowerBI

Let’s modify the PowerApp

We don’t need the gallery control – so I delete this and insert a couple of other controls

So, I insert

  • Label to show sales order number
  • Text box to enter an email address
  • Check box to select whether to include a csv file
  • A mail icon for mail using office365
  • A mail icon for mail using flow and include the csv file
  • HTML text control to construct the mail body

One of the first thing I get trapped in is when showing the SalesOrderNumber in the label control.

The PowerBIntegration object is a table with records so you have to refer to a specific record/row to get the information from the field.

Therefore, use the FILTER function or in this case the FIRST function to get the value you need.

Let’s construct a message body

We can do this by constructing a HTML text with this formula

“Dear ” & First([@PowerBIIntegration].Data).EmailAddress & “<br><br>” &

“<b> Sales Order Number: </b>” & First([@PowerBIIntegration].Data).SalesOrderNumber & “<br>”

& “Products: “

& “<table width=’100%’ border=’1′ cellpadding=’5′ style=’border:1px solid black; border-collapse:collapse’>” &

“<tr style=’background-color:#efefef’>

<th>Model</th> <th> Sales Amount </th>

</tr><tr>” &

Concat(PowerBIIntegration.Data, “<td>” & ModelName & “</td> ” & “<td>” & ‘Sales Amount’ & “</td>”, “</tr><tr>”)

& “</table>”

This will give us this table in the HTMLText control – you can expand the table to your needs.

Let’s send an email

In order to send an email without a CSV file we can use the built in Office 365 Outlook datasource via View and Data sources

After adding this we can add a formula to the OnSelect property of the mail icon.

Test the function by clicking the run button

And hit the envelope – and

We get a notification

And after a while an email

Does it work from PowerBI – yes

You have to give the app an name and save it (and publish) – and switch back to your Power BI report

And your PowerApp visual will now show the newly created app in the visual – and when we select another customer and sales order – we see the app values changes.

And when clicking the send mail icon, we get a notification and an email.

Now the tricky part – include a CSV file

In order to do this, we need help from another member of the PowerPlatform family – Microsoft Flow – this is because we cannot include files or create files using the Office 365 Outlook connector.

This can all be done using the flow – and this is the part that took me the longest time to solve.

First let’s connect a flow to the second envelope on the PowerApp screen –

You can either create a new flow or use an existing.

As this blog post is a bit long, I will show you how the final flow looks like and take you through the steps/actions in the “CreateCSVFile” flow

When I add the flow, you see that the run statement has 4 arguments

These are added in the flow via the “Ask in PowerApps” in the different steps.

Let’s look at the flow

The flow has 5 steps

The first step “PowerApps” has no settings but builds the connection to the PowerApp “click”

Next step is creating a variable called csvdataasjson.

And the argument value contains a string value that is provided by the call from PowerApps.

This will actually be a JSON string containing the records we want in our CSV file.

Next step “Parse JSON” takes the value of the variable and uses the dataoperation Parse JSON to parse the JSON string into an object

Remember to update the Schema if you change what you send to the flow via PowerApps.

The next step “Create CSV table” takes the output from “Parse JSON” and can convert the JSON to a CSV table

I have used the advanced options to show the headers.

The final step uses the “Send an email (V2)” action to send and a email

The To, Subject and Body argument uses the “Ask in PowerApps” to create arguments we can use when we run the flow in PowerApps

Call the flow from PowerApps with arguments

In PowerApp we can now activate the flow with arguments from the data using the OnSelect action on the second Mail icon

Notice the arguments the Run statement matches the PowerApps variable names in the Flow.

The initialize_value contains the a formula to construct a JSON string – if you need to include more fields from the PowerBI Dataset you simply extend the string construction.

If you check a flow run you can see the result in the Value box

Now save and republish the app and lets see if it works via PowerBI.

Let’s mail a CSV file

– A tip when moving back and forth from PowerApps to PowerBI – switch between report pages to refesh the PowerApp visual – this updates the app better than refresh of the page.

When clicking the second envelope we will activate the flow via PowerApps

And after a few seconds I receive an email

Including a CSV file with the Orderlines

To summarize

By using the power of each element of the PowerPlatform we can send an email with the selected record/s in PowerBI – but it might be a bit advanced for the citizen developer.

  • Power BI to find the data
  • PowerApps to mail a simple e-mail with the information
  • Flow to include a CSV file in the mail

Please vote for this idea if you want to make it easier to create the JSON string in PowerApps – https://powerusers.microsoft.com/t5/PowerApps-Ideas/Add-JSON-stringify-or-something-similar-for-debugging/idc-p/268352#M25884

And let me know if you have questions or feedback to my method – POWER ON !