#PowerBI – Adding a vertical bar to your slicer

Yesterday I worked on a Power BI report where the client wanted to add vertical bar next to the slicer in order to make the design of the slicers a little bit nicer and also to make the slicers recognizable cross pages by using the same coloring.

My first choice was to use a shape and group it together with the slicer

But there is an easier way.

Use the Shadow properties of the slicer.

If you choose the Custom Position

You set the

Size to 0 px

Blur to 0 px

Angle to 180 (if you want the bar to the right of the slicer you can specify 0)

Distance 10 px (determines the width of the bar)

Transparency 0 px

These settings will set the slicer to look like this

And you avoid having shapes and grouping on your page

If you change the slicer to a tile or vertical list the bar will also be there

Hope you find this useful.

Document your #powerbi model with #chatGPT and a #tabulareditor script and browse it in your model

Inspired by Darrens Gosbell’s excellent blog post – Automatically generating measure descriptions for Power BI and Analysis Services with ChatGPT and Tabular Editor – Random Procrastination (gosbell.com) – and my session at SQLBits 2023 where I showed how to call the chatGPT from within Power Query and how you can use the New chat (openai.com) – to describe your M code.

I thought it would be nice if I could add this as a description to my tables in Power BI model.

How hard can that be 🙂

Well, it turns out that we are not allowed to modify the table object via the Tabular #ditor and this includes adding a description to the table

So, if I created a script that added a description – it worked fine but returning to the desktop I was asked to refresh the queries and the descriptions where removed by Power BI desktop –

Well, what is supported as write operations to a model?

Calculation groups is supported – hmmm… could I use this and then actually create a calculation group with a calculation item for each of the tables in my model – that would also mean that I could create a table in my report and expose the descriptions in a report page instead of the description should be hidden away on the tooltip of a table in the field list – This could actually be quite awesome!

C# Script here we go

I know very little about how to write C# code so Darrens example and the documentation of Tabular editor was a big help and please bear in mind that I am a C# novice when reading my code

The steps needed to this is as follows (I have used Tabular Editor 3 but it should be possible to us the free version of Tabular Editor as well

Step 1 – Open your Power BI desktop file and find the external tool ribbon and click the Tabular Editor

Step 2 – Add a Calculation Group called “Documentation”

Step 3 – Choose to create a New C# Script

Step 3 – Use the following script and run it

#r "System.Net.Http"
using System.Net.Http;
using System.Text;
using Newtonsoft.Json.Linq;

// You need to signin to https://platform.openai.com/ and create an API key for your profile then paste that key 
// into the apiKey constant below
const string apiKey = "<YOUR_API_KEY>";
const string uri = "https://api.openai.com/v1/completions";
const string question = "Please describe this power query code for me:\n\n";

using (var client = new HttpClient()) {
    client.DefaultRequestHeaders.Clear();
    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiKey);

    foreach (var t in Model.Tables)
    {
        
        foreach ( var p in t.Partitions)
        {
            // Only uncomment the following when running from the command line or the script will 
            // show a popup after each measure
            //
            //var body = new requestBody() { prompt = question + m.Expression   };
                string _type = Convert.ToString(p.SourceType);
                string _exp = Convert.ToString(p.Expression);
        if ( _type == "M" )
            {var body = 
                "{ \"prompt\": " + JsonConvert.SerializeObject( question + p.Expression ) + 
                ",\"model\": \"text-davinci-003\" " +
                ",\"temperature\": 1 " +
                ",\"max_tokens\": 256 " +
                ",\"stop\": \".\" }";

            var res = client.PostAsync(uri, new StringContent(body, Encoding.UTF8,"application/json"));
            //res.Result.EnsureSuccessStatusCode();
            var result = res.Result.Content.ReadAsStringAsync().Result;
            var obj = JObject.Parse(result);
            var desc = obj["choices"][0]["text"].ToString().Trim(); 
          
            //Reference to your calculation group that should hold the calculation Items
            var x =(Model.Tables["Documentation"] as CalculationGroupTable).CalculationItems[t.Name];
            
            //deletes the old version
            x.Delete();

            var calculationItem1 = (Model.Tables["Documentation"] as CalculationGroupTable).AddCalculationItem();
            
            //removes any quotes in the chatGPT description
            var s = desc.Replace("\"", "");

            calculationItem1.Expression = "\"" +  s + "\"";
            calculationItem1.Name = t.Name;
            
            //Info("Processing " + t.Name) ;
            
            }
        }

    }
}

This will create a calculation item for each of your Power Query table and add a Expression that contains the chatGPT description of your M code

Example of one of the tables

Step 4 – Save the changes back to your model and you will be prompted to refresh your calculation group

Step 5 – Add a table where you take the Calculation group name and a measure that I call Query description – the value of this measure will in the table be changed to the expression of the calculation item.

https://tenor.com/embed.js

We could use the same method to document our DAX measures and put that into a calculation group as well – thereby documenting our full model and exposing the information directly in the report and not just in the tooltips of the fields.

I will try to find time to do a blog post on this as well.

Let me know in the comments if you find this useful – would very much like to hear from you.

#PowerBI Convert all Power BI links in Power Point to images

Yesterday I posted an image of some PowerPoint VBA code on LinkedIn and Twitter and a lot of people have asked for a copy of the code.

So I have made a Power Point file with the code included and a small instruction on how to use it.

The code has been changed compared to the posted image as it ran to fast for Power Point – so I added a pause in the code to wait 1 second for each conversion.

The Code

Sub ConvertAllPBIToImages()
Dim x As Slide
Dim shp As Shape

Dim sShapes As Shapes

'Loop through all the slides
For Each x In ActivePresentation.Slides

    x.Select
    'Loop through all the shapes on the slide
    For Each shp In x.Shapes

        'Is it a Power BI App - Report
        If shp.Title = "Microsoft Power BI" Then

            'Inserted to make sure as the code sometimes runs
            'a bit to fast for Power Point
            WAIT = Timer
            While Timer < WAIT + 2
               DoEvents  'do nothing
            Wend

            shp.Copy

            Set sShapes = x.Shapes

            x.Shapes.PasteSpecial ppPasteBitmap

            shp.Delete

        End If
    Next
Next

End Sub

Here is a link to a file with the code.

LINK

Hope you find it useful.

Using Field Parameters when connecting to a #PowerBI dataset or Azure Analysis Services

The new preview feature “Field parameters” in Power BI desktop has opened a lot of creativity in the Power BI community.

But if you use a connection to a Power BI dataset or Azure Analysis Services – the Parameters button will be greyed out

And if you don’t have the permission to change the model you won’t be able to use the feature.

So how can we use the Field parameter filter to make field selection or measure selection dynamically?

Well – we can use another preview feature to enable this

If you have turned “DirectQuery for PBI datasets and AS” on – you will get the option make changes to this model

Clicking this will change your connection to a direct query source instead of a live connection.

When we click add a local model – we can choose the tables and measures we want to include

And our storage mode will change to “DirectQuery”

But another thing changes as well – the New parameter – Fields will be enabled on the modelling tab

And then we can start building our Field paramaters

In this case for selecting what to appear on the X-axis and another one for selecting Measures / values

And then we can make our visuals dynamic by using our field parameters in the X-axis and Y-axis in the visual

When publishing the report, it will mean that the workspace will get an extra dataset

But don’t worry the linage is visible and as it is a direct query there is no need for setting up datarefresh

If you connect a new report to the dataset you will also get the field parameters in the new report as the information is stored in the direct query dataset.

Hope you find this useful and if you do – give this post a like please

Multivalue parameters in #PowerBI paginated report when using #PowerAutomate to export to file

I have had a couple of people posting comment on one of previous blogposts “Setup data driven report subscriptions for #PowerBI paginated reports with #PowerAutomate” asking me on how to specify values for multi value parameters in a Paginated reports.

So, I decided to write a blog post on how you can do it – if you have solved in other ways than my solution please let me know in the comments

How to do it

In my example I have created a simple report that has one multi value parameter that is called “StoreChain” and the parameter label is Chain.

And in the Power BI Report Builder the Report parameter is setup to Allow multiple values

Now let’s create the flow

I will create a flow where the user can type in the values for the Chains that the report should be filtered by – So I choose to create an instant cloud using the trigger “Manually trigger a flow”

I add an Text input called Chain and add a description to the input field

When we then select the Action “Export To File for Paginated Reports” – we can select the workspace and report we want to run

At the bottom of the action dialog we have the ParameterValues name and value section

But if we specify the values by referring to the trigger input – we will get a string value containing all the chains separated by ; (semicolon)

For example running with this

Will give us

And because there is no chain called this we will get an error when we try to test the flow

Solution

We need to specify the parameter values a little bit different

If you click on the button “Switch to input entire array” we can specify the Parameter values as an array instead

And this reveals that in order to specify more than one chain we need to specify an array of values containing a column called name and value – like this.

This means that before we call the action to export the paginated report we need to build an array of the specified chains (parameter values).

First I create an array variable called pv containing an empty array

Next, I use an “Apply to each” where I use the split function to create an array of the text specified in the trigger.

And inside the Apply to each step I build the parameter value array by using the action “Append to array variable” where I construct an item in the pv array for each of the result of the split function

Then I can use the pv Array variable to Export To File for Paginated Reports – ParameterValues setting

And then to finish the flow we can add a “Send an email (V2)” action to send the report

The final flow will have the following steps.

Next – let us test it

With this entered as text for the input

The steps will specify the ParameterValues as an array

And after about 35 seconds the flow has emailed me the requested reports

Success 🙂

Hope you find this useful.

Using Calculation Groups in #PowerBI to implement a Many 2 Many (M2M) filter

Yesterday I tweeted about how I solved the implementation of a Many 2 Many filter using Calculation groups.

And was encouraged to write a blog post about this – so here it is 😊.

The scenario

Imagine a simple star schema where we have a fact table with sales and a customer and Date dimension.

And a in the demo I have also created a few measures – (in my actual customer case I had over 100 measures)

Now we want to expand the model to handle that a customer can belong to one or more customer groups –

This is done by adding the table Customer Groups that contains the Customer Groups and then a bridge table that contains the link between the customers to the different customers groups.

For instance, Customer Key 12031 both belongs to Customer Group 1 and 2.

But when we try to report on the Sales Value the value for the groups will be identical

This is because row context comes from the table Customer Groups and because the filter from that table can’t be passed to the customer table as the cross filter direction points the other way (red circle)

We could change the cross filter direction to Both via the Edit relationship dialog

And it would solve the problem.

But as Alberto Ferrari highlights in this blog post – Bidirectional relationships and ambiguity in DAX – SQLBI – this can be dangerous.

Change all the measures

For all our measures to work I would have to update them all and add a CROSSFILTER statement each of the measure statement

This will give me the result I want but also some work to update all measures.

Use Calculation groups to add filter to all your measures

Instead let’s create a Calculation Group to apply the filter – and the tool to do this is the Tabular Editor – Tabular Editor

We will add a Calculation Group via Tables

And add a group called “Customer Group Filter”

Next step is to add a Calculation Item

I will call it “Apply Customer Group Filter”

And then in the expression editor I will add the expression that applies the CROSSFILTER to the SELECTEDMEASURE().

Clicking Save and returning to Power BI Desktop will prompt me to refresh the calculation groups

Now I will have a Calculation Group Filter in my Fields list

And we can add a slicer or a filter to the visual, page or all pages

And if we select the Calculation Item we can see the filter is being applied.

You can download the demo file – here

Summary

Typically, the calculation groups examples are normally focused on Time intelligence but absolutely not limited to time – I hope this example can give you some inspiration to use Calculation Groups in other scenarios as well.

In my actual case I saved time on updating over 100 measures – so I really ❤ calculation groups.

Let me know what you think and if you find it useful as well.

#PowerBI – Change the data source in your composite model with direct query to AS/ Power BI Dataset

I have been playing around with the new awesome (preview) feature in the December Power BI Desktop release where we can use DirectQuery for Power BI datasets and Azure Analysis services (link to blogpost)

In my case I combined data from a Power BI dataset, Azure Analysis Services, and a local Excel sheet. The DirectQuery sources was in a test environment.

I then wanted to try this on the actual production datasets and wanted to change the datasources – and was a bit lost on how to do that but luckily found a way that I want to share with you.

Change the source

First you click on Data source settings under Transform data

This will open the dialog for Data source settings and show you the list of Data sources in the current file.

Now you can either right click the data source you want to change

Or click the button “Change Source…”

Depending on your data source different dialogs will appear

This one for my Azure Analysis Services Connection

And this one for Power BI Dataset

And this one for the Local Excel workbook

Hope this can help you to.

Happy new year to you all.

You must know about this shortcut key in #PowerBI Desktop

Working with the field list on a large model in Power BI Desktop can quickly make you end up with a lot of expanded tables and you collapsing them one by one.

Don’t do that

Even though that is good if you want to improve your chances of beating your kids in Fortnite – it probably won’t – so instead do one of the following

If you want to use your mouse

Click the show/hide pane in the header of the Fields panel

This will collapse all expanded tables in the field list at once – plus if you have used the search field – it will clear that as well.

But you want to do it using the keyboard use

ALT + SHIFT + 1

This will collapse all the expanded tables as well.

Here is a link to the documentation about short cut keys in Power BI desktop – run through them – there might be some that can save you a click or two

Keyboard shortcuts in Power BI Desktop – Power BI | Microsoft Docs

Use hidden measures and members from #PowerBI dataset in an Excel Pivot table

When you connect to a Power BI Dataset from Power BI desktop you might have noticed that you can see and use hidden measures and columns in the dataset.

But the hidden fields cannot be seen if you browse the dataset in Excel.

But that does not mean that you cannot use the fields in Excel – and here is how you can do it.

Using VBA

You can use VBA by creating a macro

The code will add the field AddressLine1 from the DImReseller dimension as a Rowfield if the active cell contains a pivotable.

Sub AddField()
    Dim pv As PivotTable
        Set pv = ActiveCell.PivotTable
        pv.CubeFields("[DimReseller].[AddressLine1]").Orientation = xlRowField
End Sub

If you want to add a measure/value to the pivotable you need to set change the Orientation property to xlDataFields

This means that we now have added two hidden fields from the dataset

Add hidden measures using OLAP Tools

You can also add hidden measures using the OLAP Tools and MDX Calculated Measure

Simply create a new calculated measure by referencing the hidden measure in the MDX

This will add a calculated Measure to the measure group you selected

And you can add that to your pivotable

Referencing hidden items using CUBE functions

Notice that you can also reference the hidden measures using CUBE functions

Simply specify the name of the measure as the member expression in this case as “[Measures].[Sales Profit]”

You can also refer to members from hidden fields using the CUBEMEMBER functions

Hope this can help you too.

Power On!

Spot the difference between Power BI Desktop and Power BI Desktop (Store Version) #PowerBI

On my computer I have 2 versions of Power BI Desktop installed – one from the Microsoft Store which is updated automatically and the downloaded version from downloads – and typically I have last month edition as my downloaded version.

But in my taskbar its impossible to tell the difference between the two.

Well we can solve that by changing the icon for the downloaded version – its not possible for the store version.

If you right click the icon in the taskbar and then right click the Power BI Desktop

You can select the properties for this App.

Now click the Change Icon

This will show you the current icon and now you can change this by clicking Browse – in my case I will select the icon for the PBIDocument

And click open – then icon will now be set to this

And when clicking OK

We will see the icon has changed for the Shortcut.

Notice that it will change immediately

But after a restart it will appear

Hope this can make your choice of Power BI Desktop versions easier for you as well.