Forum Discussion

Applicable88's avatar
Applicable88
Impactful Individual
4 years ago
Solved

Alternating Attributes in new columns

Hello,

 

I have a mastercalendar as an example. I didn't show all dates just the calendarweeks for simplicity here:

CalendarweekCategory A Category B Category C
1 A B C
2 B C A
3 C A B
4 A B C

 

I want category alternating for the weeks to come. So in week one of Category A it starts with A and then B and then C and again A.

Its a regular alternating pattern. How to write this as a calculated column and also starts the other categories with B for week 1 repsectively C for Category C?

 

Thank you very much in advanced. 

Best. 

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    4 years ago
    Category A
    = {"A","B","C"}{Number.Mod([Calendarweek]-1,3)}
    
    Category B
    = {"B","C","A"}{Number.Mod([Calendarweek]-1,3)}
    
    Category C
    = {"C","A","B"}{Number.Mod([Calendarweek]-1,3)}

    See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxmDSBEyagkkzMGkOJi3ApCWYNDSAUBDdhhDthhD9hkADYgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Calendarweek = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Calendarweek", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Category A", each {"A","B","C"}{Number.Mod([Calendarweek]-1,3)}),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Category B", each {"B","C","A"}{Number.Mod([Calendarweek]-1,3)}),
        #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Category C", each {"C","A","B"}{Number.Mod([Calendarweek]-1,3)})
    in
        #"Added Custom2"

     

3 Replies

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Most Valuable Professional

    Since you are saying calculated column, hence I am assuming you need DAX solution

    For Category A
    =SWITCH(MOD(Table1[Calendarweek]-1,3),0,"A",1,"B",2,"C")
    
    For Category B
    =SWITCH(MOD(Table1[Calendarweek]-1,3),0,"B",1,"C",2,"A")
    
    For Category C
    =SWITCH(MOD(Table1[Calendarweek]-1,3),0,"C",1,"A",2,"B")
    • Applicable88's avatar
      Applicable88
      Impactful Individual

      Vijay_A_Verma thanks for your quick reply. Sorry that I wasn't clear. I wanted to see also an alternative in PowerQuery in a added column. I want to compare a DAX and a PowerQuery M approach. 

       

      Hope you can also show how it would look like in PowerQuery when I add a new column. (Not calculated column here)

      Thanks!

      • Vijay_A_Verma's avatar
        Vijay_A_Verma
        Most Valuable Professional
        Category A
        = {"A","B","C"}{Number.Mod([Calendarweek]-1,3)}
        
        Category B
        = {"B","C","A"}{Number.Mod([Calendarweek]-1,3)}
        
        Category C
        = {"C","A","B"}{Number.Mod([Calendarweek]-1,3)}

        See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxmDSBEyagkkzMGkOJi3ApCWYNDSAUBDdhhDthhD9hkADYgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Calendarweek = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"Calendarweek", Int64.Type}}),
            #"Added Custom" = Table.AddColumn(#"Changed Type", "Category A", each {"A","B","C"}{Number.Mod([Calendarweek]-1,3)}),
            #"Added Custom1" = Table.AddColumn(#"Added Custom", "Category B", each {"B","C","A"}{Number.Mod([Calendarweek]-1,3)}),
            #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Category C", each {"C","A","B"}{Number.Mod([Calendarweek]-1,3)})
        in
            #"Added Custom2"