Search code examples
daxpowerquerypowerbi-desktopm

Convert Switch True() Dax calculated column to M query custom colum


I am having issues with my calculated column and the multiple tables I am joining. It is not filtering my visuals correctly. After researching it was recommended to use a custom column in the query instead but I do not know where to start to convert the following DAX to M query.

overall = 
VAR skills =
    CALCULATETABLE (
        VALUES ( tsr_skill[ts_skill] ),
        ALLEXCEPT ( tsr_skill, tsr_skill[ts_tsr] )
    )
RETURN
    SWITCH (
        TRUE (),
        "JMSR" IN skills, "Senior",
        "JMOV" IN skills, "Over",
        "JMUN" IN skills, "Under",
        "JMRH" IN skills, "RHT",
        "MNT"
    )

Data structure in Query:

1

How I would like the data to show in the Query instead of showing as a calculated column.

Preferred Output:

2


Solution

  • Based on your explanation, and the levels assigned in your DAX formula, it would seem that all should be assigned as "under".

    In your "Preferred Output" you do show JMXX being assigned as "Over", but that tsr does not include the JMOV skill

    If your written explanation is correct, and your Preferred Output screenshot incorrect based on the posted data, then, in PQ you can

    • Group by tsr
    • Create a custom aggregation returning the "overall" based on containing one of the skills listed in your DAX formula.

    If that is not the case, please clarify how you are assigning "Over" to JMXX.

    Edit: M Code simplified

    M Code

    let
        //Source = the data structure you show
        Source = Excel.CurrentWorkbook(){[Name="Table13"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ts_tsr", type text}, {"ts_skill", type text}}),
    
    //Group rows by tsr, then check if it has one of the defined skills
    //If so, return the appropriate ranking.
        #"Grouped Rows" = Table.Group(#"Changed Type", {"ts_tsr"}, {
            {"ALL", each _, type table [ts_tsr=nullable text, ts_skill=nullable text]},
    
            {"overall", each if List.Contains([ts_skill],"JMSR") then "Senior"
                            else if List.Contains([ts_skill],"JMOV") then "Over"
                            else if List.Contains([ts_skill],"JMUN") then "Under"
                            else if List.Contains([ts_skill],"JMRH") >=0 then "RHT"
                            else "MNT"}
           }),
           
    //Then re-expand the table
        #"Expanded ALL" = Table.ExpandTableColumn(#"Grouped Rows", "ALL", {"ts_skill"}, {"ts_skill"})
    in
        #"Expanded ALL"
    

    Data
    enter image description here

    Output
    enter image description here