Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
GlassShark1
Helper III
Helper III

Power BI equivalent to python lists / dictionaries?

I'm just curious if something similar to the functionality of lists/dictionaries in python exists in Power BI.

 

Quite often I want to do things like 'If the selected area is in X list', but i don't think this kind of thing is replicated in PBI anywhere? What i typically do instead is create some kind of lookup table that has a column indicating if the area is in a group or not.

 

A quick example, I wanted to figure out how to specify different aggregation types according to slicer value (e.g. tell a measure to sum values if it makes sense to sum them, but if the value selected requires an average instead of a sum, recognse that and do that instead. In python you might do this in a dictionary, something like the below to tell a formula how to handle different values:


Aggregation_Type_to_Use = {
"Number of houses" : "sum",

"House Price" : "average"}

 

Is there any way to do this in a simlar way or would you use lookup tables instead? In the example above, i'm guessing i'd need to add a column indicating the correct aggregation type and reference that in a formula? Something like the below quick example?

 

Answer = 

var agg_type_column = max('FactTbl['Aggregation type to use'])
var sum_measure = sum('FactTbl'[value])
var avg_measure = average('FactTbl'[value])
return if(agg_type_column = 'sum', sum_measure, average_measure)

Thanks

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @GlassShark1 

 

According to your quick example at the end, it is possible. But the 'Aggregation type to use' column should be from an individual table rather than the fact table. That table should be disconnected from the fact table. Then use a measure like 

Answer =
VAR agg_type_column = MAX ( 'Types'[Aggregation type to use] )
RETURN
    SWITCH ( agg_type_column, "sum", [sum_measure], "average", [average_measure] )

 

Here is a blog which has a detailed explanation about this solution: Change the Column or Measure Value in a Power BI Visual by Selection of the Slicer: Parameter Table ... 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @GlassShark1 

 

According to your quick example at the end, it is possible. But the 'Aggregation type to use' column should be from an individual table rather than the fact table. That table should be disconnected from the fact table. Then use a measure like 

Answer =
VAR agg_type_column = MAX ( 'Types'[Aggregation type to use] )
RETURN
    SWITCH ( agg_type_column, "sum", [sum_measure], "average", [average_measure] )

 

Here is a blog which has a detailed explanation about this solution: Change the Column or Measure Value in a Power BI Visual by Selection of the Slicer: Parameter Table ... 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

CNENFRNL
Community Champion
Community Champion

Just like the fact that there doesn't now exist a native func of SWITCH/CASE in python, you can't expect all syntactic equivalents in another language. Just paste mock-up dataset and articulate what you expect.


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.