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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare 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
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

Check out the October 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

Find out what's new and trending in the Fabric Community.