Forum Discussion

LYorkToenniges's avatar
LYorkToenniges
Helper III
2 years ago
Solved

Switch in Power Query?

I followed a formula found on this forum, confirmed as the solution to changing a column of month names to date format in a new column and Power Query is rejecting the use of SWITCH.  Why is this a confirmed answer if PQ doesn't recognise the function?

 

This is what I have, copied almost verbatum (Col names changed is all):

= Table.AddColumn(#"Changed Type4", "Month Num", each SWITCH([Month],
"January", Format (Date(2020,1,1), m),
"February", Format (Date(2020,2,1), m),
"March", Format (Date(2020,3,1), m),
"April", Format (Date(2020,4,1), m),
"May", Format (Date(2020,5,1), m),
"June", Format (Date(2020,6,1), m),
"July", Format (Date(2020,7,1), m),
"August", Format (Date(2020,8,1), m),
"September", Format (Date(2020,9,1), m),
"October", Format (Date(2020,10,1), m),
"November", Format (Date(2020,11,1), m),
"December", Format (Date(2020,12,1), m)
))

 

I need ot make a cumulative sum column by month (ie, Total for January, then total for Jan and Feb, so on).  All I have is the month name in the source data.  If anyone know what I'm doing wrong please help.

  • Hi LYorkToenniges,

     

    That solutions appears to be a calculated column (DAX) not a custom column (Power Query). To get the month number from a month name, enter this in to the formula area of the Add Custom Column dialog box:

    Date.Month( Date.From( [Month] & "- 2020"))

     

    Based on your example I assumed your month name field is called: [Month]  by appending a year, the string can be converted into a date value from which a month number can be obtained.

    I hope this is helpful

     

3 Replies

  • m_dekorte's avatar
    m_dekorte
    Resident Rockstar

    Hi LYorkToenniges,

     

    That solutions appears to be a calculated column (DAX) not a custom column (Power Query). To get the month number from a month name, enter this in to the formula area of the Add Custom Column dialog box:

    Date.Month( Date.From( [Month] & "- 2020"))

     

    Based on your example I assumed your month name field is called: [Month]  by appending a year, the string can be converted into a date value from which a month number can be obtained.

    I hope this is helpful

     

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    LYorkToenniges m_dekorte is absolutely correct. SWITCH is a DAX function, there is no native switch or case function in Power Query. Please reply with a link to the forum post you found so that I can unmark the post as the solution because it is clearly not correct.