Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Create IF/THEN statement in DAX using parameter

I have to create a dynamic field that basically says "if option A, return x.  If option B, return y"   X and Y are 2 different fields (columns) within the same record in my query.   I have create...
  • Anonymous's avatar
    Anonymous
    6 years ago

    HI Anonymous ,

    I'd like to clarify some features you misunderstand:

    Power bi contains multiple data levels:  query table(query, custom function, query parameters) -> data model(table, calculate table, calculate column) -> data view(visual, filter, measure)  

    These different levels are generated from left to right, you can use parent to effect its child but the child not able to affect the parent level.

    For this scenario, it is impossible to use data view filter/slicers to interact with query parameters or create dynamic calculate column/table based on data view filter/slicers. (query parameters can interact with query table records, it will also affect generated data model tables and data view tables)

    In summary, you can set query parameters and custom column on the query table side with if statement to dynamic change custom column value based on query parameter values or create a measure on the data view side to integrate with slicer to display dynamic result.

    Dynamic measure(DAX) :

    My Favorite DAX Feature: SELECTEDVALUE with SWITCH 

    M query(power query) :

    #"Added Custom" = Table.AddColumn(#"previous step", "Dynamic", each if Parameter= "A" then [ColumnA] else if Parameter= "B" then [ColumnB] else null)

    Regards,

    Xiaoxin Sheng