Conditional columns in SUMMARIZE...

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,717
Hi Team,

I have a use case at the moment where I need to create a new table (inside a measure) that considers a parameter set by the user.

I know I can do the following:

Code:
VAR Scenario A =
    SUMMARIZE (
        Table1,
        Table1[Shop],
        Table1[Product],
        Table1[Week]
    )

VAR Scenario B =
    SUMMARIZE (
        Table1,
        Table1[Shop],
        Table1[Category],
        Table1[Week]
    )

But what I'm wondering is whether it's possible to do something like this:

Code:
VAR Scenario =
    SUMMARIZE (
        Table1,
        Table1[Shop],
        IF ( SELECTEDVALUE ( Table2[Selection] ) = "Product", Table1[Product], Table1[Category] )
        Table1[Week]
    )

Which isn't syntactically valid in its current form, but hopefully illustrates what I would like to be able to do, i.e. to decide what is or isn't included within SUMMARIZE.

Hoping for some guidance, even if it's just to confirm that this kind of approach isn't possible.

Cheers,

Matty
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Why are you using summarize inside a measure? It can be done, but I’ve seen more unnecessary use than necessary use.

you can’t do it the way you have shown. if you’re using PBI, you could investigate if the new field parameters feature will do this.
 
Upvote 0
Why are you using summarize inside a measure? It can be done, but I’ve seen more unnecessary use than necessary use.

you can’t do it the way you have shown. if you’re using PBI, you could investigate if the new field parameters feature will do this.
Hi Matt,

Thanks for responding. And yes, I'm using PBI.

Here's the measure SUMMARIZE is part of:

Code:
ABS Difference =
VAR Choice =
    SELECTEDVALUE ( 'Table2'[Choice] )
VAR WithProduct =
    SUMMARIZE (
        Table1,
        Table1[Shop],
        Table1[Product],
        Table1[Week]
    )
VAR WithCategory =
    SUMMARIZE (
        Table1,
        Table1[Shop],
        Table1[Category],
        Table1[Week]
    )
VAR Result =
    IF (
        Choice = "Product",
        SUMX (
            WithProduct,
            CALCULATE (
                ABS (
                    SUM ( Table1[Forecast] )
                        - SUM ( Table1[Orders] )
                )
            )
        ),
        SUMX (
            WithCategory,
            CALCULATE (
                ABS (
                    SUM ( Table1[Forecast] )
                        - SUM ( Table1[Orders] )
                )
            )
        )
    )
RETURN
    Result

So, the measure takes the choice taken by the user and calculates the absolute difference based on the chosen grouping. The measure works well, but I'm just wondering whether there's a better way to write it.

Cheers,

Matty
 
Upvote 0
can you please post an image of the data model, and also post an image of a matrix/table showing some results. It is not possible to effectively write DAX without these things
 
Upvote 0
Hi Matt,

The data model is very simple...

Table1 looks as follows:

ShopCategoryProductWeekForecastOrders
10001A
1​
10055
10001A
2​
150135
10001A
3​
100110
10001B
1​
5075
10001B
2​
5010
10001A
1​
5010
10001A
2​
2565
10001B
1​
6045
10001B
2​
8085
10001B
3​
220375
10002C
1​
4535
10002C
2​
2535
10002C
3​
2320
10002D
1​
1519
10002D
2​
510
10002C
1​
1815
10002C
2​
1210
10002D
1​
4550
10002D
2​
610
10002D
3​
45
1,0831,174

Table2 is an isolated table (no relationships in place) with just 2 rows:

Choice
Category
Product

The measure I presented returns the following when 'Category' is chosen (see results in the ABS Difference column):

ShopCategoryWeekForecastOrdersABS Difference
10001
1​
26018575
10001
2​
30529510
10001
3​
320485165
10002
1​
1231194
10002
2​
486517
10002
3​
27252
1,0831,174273

And when 'Product' is chosen, it returns the following:

ShopProductWeekForecastOrdersABS Difference
100A
1​
1506585
100A
2​
17520025
100A
3​
10011010
100B
1​
11012010
100B
2​
1309535
100B
3​
220375155
100C
1​
635013
100C
2​
37458
100C
3​
23203
100D
1​
60699
100D
2​
11209
100D
3​
451
1,0831,174363

So, based on the level of aggregation chosen, you will see a different result at the total level (273 when 'Category' is chosen; 363 when 'Product' is chosen).

The measure I wrote works; I'm just interested to understand whether other methods are possible.

Cheers,

Matty
 
Upvote 0
Power BI is not the same as a relational database and you should take a different approach

You should work on you data structure first, then come back to the problem.
Table 1 should not contain category - remove that.
Table 2 should not exist in this format
You need a new table 3 containing the unique product codes (primary key), and Category (and cat code if you like).
Join table 1 to table 3 on product code, 1 to many
hide the product code in table 1 and do not use it.

Use Table 3 columns to add to your visual (axis, table rows, etc)
the measures for orders and forecast are then simply SUM() of the columns. No summarize, calculate, selected value needed
 
Upvote 0
Hi Matt,

Thanks for responding.

The thing I'm looking for some direction on does not relate to the 'Forecast' or 'Orders' fields in themselves as such - it's the absolute difference between them at different levels of aggregation that I'm interested in optimising DAX-wise.

I've mocked up a quick PBIX that hopefully illustrate things better. The below snips show the different (but expected) results we get based on the aggregation level chosen:

At Category level:

1660725784253.png


At Product level:

1660725817310.png


Measures used are as follows, for the sake of clarity:

Code:
Forecast Quantity =
SUM ( Table01[Forecast] )

Code:
Order Quantity =
SUM ( Table01[Orders] )

Code:
Forecast Accuracy =
1 - DIVIDE ( [ABS Difference], [Order Quantity], 0 )

The 'ABS Difference' measure is as per post #3.

Is there a better way to write the 'ABS Difference' measure? I'm open to simplifying the Fact table and having a separate Dim table for Category if that's the right approach.

Cheers,

Matty
 
Upvote 0
Did you build the model as I suggested? Can you please post it online (say dropbox or similar) and share the link here? Regardless, one solution to forecast accuracy would be something like

Code:
= 1  - DIVIDE (
               SUMX ( Table01, ABS ( Table01[Forecast] - Table01[Orders] ) ),
               [Order Quantity],
               0
    )

there may be a more efficient version, but I wouldn't attempt that without the model.
 
Upvote 0
Hi Matt,

You can get the test *.pbix file I created from this Link.

Happy to be advised otherwise, but I can't see how see this could be achieved any other way. The thing I am interested in is how a table can be built dynamically based on user input, as per my original post.

Cheers,

Matty
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top