cancel
Showing results for 
Search instead for 
Did you mean: 
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
Super User
Super User

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 beyond their comprehension!

DAX is simple, but NOT EASY!

Helpful resources

Announcements
September Update

Check it Out!

Click here to learn more about the September 2022 updates!

Power BI Dev Camp Session 26

Check it Out!

Mark your calendars and join us on Thursday, September 29 at 11a PDT for a great session with Ted Pattison!

Top Solution Authors