Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Split multiple columns into rows using DAX

Hi Jihwan_Kim & other DAX experts,

 

Thanks Jihwan_Kim for your help earlier.

 

My query is,

The condition to split is there are multiple columns to be splitting into rows. I have done splitting from 'Raw Data' table into rows in Table A for column 'List of Students', as below image and I would like to do as per in Table A+ (add new calculated column for the Activity) where it has multiple values. E.g. John has multiple activities.

 

Really appreciate if anyone can help me to achieve from Table A to Table A+. 


Note: I have done using Power Query before, but due to performance issue I have to move to DAX instead

 

 

Thank you

  • Hi,

    I am not sure if I understood your question correctly, but please check the below and the attached pbix file.

     

    Expected result table = 
    VAR _t =
        GENERATESERIES ( 1, 100, 1 )
    VAR _pathitem =
        ADDCOLUMNS ( Data, "@path", SUBSTITUTE ( Data[List of Students], ",", "|" ) )
    VAR _pathitemsinging =
        ADDCOLUMNS ( Data, "@pathsinging", SUBSTITUTE ( Data[Singing], ",", "|" ) )
    VAR _pathitemdancing =
        ADDCOLUMNS ( Data, "@pathdancing", SUBSTITUTE ( Data[Dancing], ",", "|" ) )
    VAR _generatetable =
        FILTER (
            GENERATE (
                _t,
                ADDCOLUMNS ( _pathitem, "@Students", PATHITEM ( [@path], [Value] ) )
            ),
            [@Students] <> BLANK ()
        )
    VAR _generatesinging =
        SUMMARIZE (
            FILTER (
                GENERATE (
                    _t,
                    ADDCOLUMNS ( _pathitemsinging, "@sing", PATHITEM ( [@pathsinging], [Value] ) )
                ),
                [@sing] <> BLANK ()
            ),
            Data[ID],
            Data[Class Name],
            [@sing]
        )
    VAR _generatedancing =
        SUMMARIZE (
            FILTER (
                GENERATE (
                    _t,
                    ADDCOLUMNS ( _pathitemdancing, "@dance", PATHITEM ( [@pathdancing], [Value] ) )
                ),
                [@dance] <> BLANK ()
            ),
            Data[ID],
            Data[Class Name],
            [@dance]
        )
    RETURN
        SUMMARIZE (
            ADDCOLUMNS (
                _generatetable,
                "@activity",
                    SWITCH (
                        TRUE (),
                        COUNTROWS (
                            FILTER (
                                _generatesinging,
                                Data[ID] = EARLIER ( Data[ID] )
                                    && Data[Class Name] = EARLIER ( Data[Class Name] )
                                    && [@Students] = [@sing]
                            )
                        ) >= 1 && COUNTROWS (
                            FILTER (
                                _generatedancing,
                                Data[ID] = EARLIER ( Data[ID] )
                                    && Data[Class Name] = EARLIER ( Data[Class Name] )
                                    && [@Students] = [@dance]
                            )
                        ) >= 1, "Sing,Dance",
                        COUNTROWS (
                            FILTER (
                                _generatesinging,
                                Data[ID] = EARLIER ( Data[ID] )
                                    && Data[Class Name] = EARLIER ( Data[Class Name] )
                                    && [@Students] = [@sing]
                            )
                        ) >= 1, "Sing",
                        COUNTROWS (
                            FILTER (
                                _generatedancing,
                                Data[ID] = EARLIER ( Data[ID] )
                                    && Data[Class Name] = EARLIER ( Data[Class Name] )
                                    && [@Students] = [@dance]
                            )
                        ) >= 1, "Dance"
                    )
            ),
            Data[ID],
            Data[Class Name],
            [@Students],
            [@activity]
        )

6 Replies

  • Hi,

    I am not sure if I understood your question correctly, but please check the below and the attached pbix file.

     

    Expected result table = 
    VAR _t =
        GENERATESERIES ( 1, 100, 1 )
    VAR _pathitem =
        ADDCOLUMNS ( Data, "@path", SUBSTITUTE ( Data[List of Students], ",", "|" ) )
    VAR _pathitemsinging =
        ADDCOLUMNS ( Data, "@pathsinging", SUBSTITUTE ( Data[Singing], ",", "|" ) )
    VAR _pathitemdancing =
        ADDCOLUMNS ( Data, "@pathdancing", SUBSTITUTE ( Data[Dancing], ",", "|" ) )
    VAR _generatetable =
        FILTER (
            GENERATE (
                _t,
                ADDCOLUMNS ( _pathitem, "@Students", PATHITEM ( [@path], [Value] ) )
            ),
            [@Students] <> BLANK ()
        )
    VAR _generatesinging =
        SUMMARIZE (
            FILTER (
                GENERATE (
                    _t,
                    ADDCOLUMNS ( _pathitemsinging, "@sing", PATHITEM ( [@pathsinging], [Value] ) )
                ),
                [@sing] <> BLANK ()
            ),
            Data[ID],
            Data[Class Name],
            [@sing]
        )
    VAR _generatedancing =
        SUMMARIZE (
            FILTER (
                GENERATE (
                    _t,
                    ADDCOLUMNS ( _pathitemdancing, "@dance", PATHITEM ( [@pathdancing], [Value] ) )
                ),
                [@dance] <> BLANK ()
            ),
            Data[ID],
            Data[Class Name],
            [@dance]
        )
    RETURN
        SUMMARIZE (
            ADDCOLUMNS (
                _generatetable,
                "@activity",
                    SWITCH (
                        TRUE (),
                        COUNTROWS (
                            FILTER (
                                _generatesinging,
                                Data[ID] = EARLIER ( Data[ID] )
                                    && Data[Class Name] = EARLIER ( Data[Class Name] )
                                    && [@Students] = [@sing]
                            )
                        ) >= 1 && COUNTROWS (
                            FILTER (
                                _generatedancing,
                                Data[ID] = EARLIER ( Data[ID] )
                                    && Data[Class Name] = EARLIER ( Data[Class Name] )
                                    && [@Students] = [@dance]
                            )
                        ) >= 1, "Sing,Dance",
                        COUNTROWS (
                            FILTER (
                                _generatesinging,
                                Data[ID] = EARLIER ( Data[ID] )
                                    && Data[Class Name] = EARLIER ( Data[Class Name] )
                                    && [@Students] = [@sing]
                            )
                        ) >= 1, "Sing",
                        COUNTROWS (
                            FILTER (
                                _generatedancing,
                                Data[ID] = EARLIER ( Data[ID] )
                                    && Data[Class Name] = EARLIER ( Data[Class Name] )
                                    && [@Students] = [@dance]
                            )
                        ) >= 1, "Dance"
                    )
            ),
            Data[ID],
            Data[Class Name],
            [@Students],
            [@activity]
        )
  • 123abc's avatar
    123abc
    Community Champion

    Certainly! To achieve matching based on values in DAX, you can use relationships and create calculated columns or measures depending on your requirements. Assuming you have two tables, 'Achieved' and 'To Achieve', and they share a common column (let's say 'ID' for demonstration purposes), you can create relationships between them. Then, you can use DAX formulas to achieve matching.

    Here's an example using calculated columns:

     

    Table 3 ('To Achieve') =
    VAR CurrentID = 'To Achieve'[ID]
    RETURN
    CALCULATE(
    MAX('Achieved'[Value]),
    'Achieved'[ID] = CurrentID
    )

     

    This formula assumes that 'To Achieve' and 'Achieved' tables have a relationship based on the 'ID' column. It looks up the corresponding 'Value' from the 'Achieved' table for each row in the 'To Achieve' table.

    If you need to create a measure instead, you can use similar logic within the CALCULATE function.

    Remember to adjust the column and table names according to your actual data structure. If you have multiple columns to match on, you might need to adjust the formula accordingly.

     

     

    If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

     

    In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi 123abc & thank you for the response.

       

      I want to achieve from table A to table A+ (add new calculated column named 'Activity Involved'.

       

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

    Did the above suggestions help with your scenario? if that is the case, you can consider Kudo or Accept the helpful suggestions to help others who faced similar requirements.

    If these also don't help, please share more detailed information and description to help us clarify your scenario to test.

    How to Get Your Question Answered Quickly 

    Regards,

    Xiaoxin Sheng