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...
  • Jihwan_Kim's avatar
    2 years ago

    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]
        )