Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
kalleeljas
Frequent Visitor

if statement in group by Power Query

Hi.

 

I would like to group a table on City, Tech and Date with different aggregations based on a value, "Type".

 

My table looks something like this:

ServiceärendenrCityTechDateHoursType
415132ÄLVDALEN30312017-02-031Work
415132ÄLVDALEN30312017-02-0435Travel
415673AVESTA30312017-02-062Travel
415673AVESTA30312017-02-0615Work
417751FALUN30312017-02-175Travel
417751FALUN30312017-02-171Work
418018ENVIKEN30312017-02-201Work
418018ENVIKEN30312017-02-202Travel
418329FALUN30312017-02-221Work
418329FALUN30312017-02-225Travel
418313FALUN30312017-02-221Work
418313FALUN30312017-02-225Travel

 

Now, what i would like to do is to Sum "Hours" where the "Type" is "Work", and get Max where the value is "Travel"

So i should get total 2,5 hours in "Falun" on the date 2017-02-22.

 

Im trying to do solve it like this:

 

Table.Group(Table, {"Date", "City", "Tech"}, {{"Hours", each if [Type] = "Travel" then List.Max([Hours]) else List.Sum([Hours])}})

But i always end up in the "else" statement and get this result:

 

bild.png

As you can see the total is 3, and i want it to be 2,5

 

is there something wrong with my "if" statement or is there another way to achive this?

 

Thanks

// Kalle Eljas

 

 

 

4 REPLIES 4
MarcelBeug
Community Champion
Community Champion

My suggestion is to create new columns for work hours and travel hours, then do the grouping (summing the work hours and taking the max of travel hours) and finally add up the Work and Travel hours (at least, that's what I understand you try to achieve).

The code below is created using the menu-options, but I adjusted the code for step #"Inserted Addition" to use List.Sum (instead of +) so null values are counted as 0.

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Serviceärendenr", Int64.Type}, {"City", type text}, {"Tech", Int64.Type}, {"Date", type date}, {"Hours", Int64.Type}, {"Type", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "TravelHours", each if [Type] = "Travel" then [Hours] else null),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "WorkHours", each if [Type] = "Work" then [Hours] else null),
    #"Grouped Rows" = Table.Group(#"Added Custom1", {"Date", "City", "Tech"}, {{"SumHours", each List.Sum([WorkHours]), type number}, {"MaxHours", each List.Max([TravelHours]), type number}}),
    #"Inserted Addition" = Table.AddColumn(#"Grouped Rows", "Sum", each List.Sum({[SumHours],[MaxHours]}), type number),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Addition",{"SumHours", "MaxHours"})
in
    #"Removed Columns"

 

Specializing in Power Query Formula Language (M)

Not recommended, but just to illustrate how it can be done with an if...then...else statement in group by:

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Serviceärendenr", Int64.Type}, {"City", type text}, {"Tech", Int64.Type}, {"Date", type date}, {"Hours", Int64.Type}, {"Type", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Date", "City", "Tech"}, 
                      {{"Hours", 
                      each List.Sum(
                        {List.Sum(Table.AddColumn(_,"SumHours", each if [Type]="Work"   then [Hours] else null)[SumHours]),
                         List.Max(Table.AddColumn(_,"MaxHours", each if [Type]="Travel" then [Hours] else null)[MaxHours])}),
                      type number}})
in
    #"Grouped Rows"
Specializing in Power Query Formula Language (M)
Anonymous
Not applicable

@MarcelBeug what if i want to add two columns, for max and min.

I have tried this but getting error.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZDPCoJAGMTfZc8K+0ddO0oaReJFs4N4EFtICo3NBO89Sm/ii/XtIag0NE/jN+zPGSZJUChkU+Sie0hRHkQpkYaWRd2CRCI/grhZLUDW1U1eldteBEq1BBnEJIyC09392HV8L4BvhhkBoZhwHVMdMziUsa/kaTplKM9UYTJrxPkFWlz9zom9MHIGKEsdcyBifjbk3FRvVo6/G2hHOBy9chOYrx1sTGxwvCDebIdWoHgm09vAZnTxuxulAznjRG8BmxH2b8oo8Z6SPgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t, #"(blank).4" = _t, #"(blank).5" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}, {"(blank).4", type text}, {"(blank).5", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Serviceärendenr", Int64.Type}, {"City", type text}, {"Tech", Int64.Type}, {"Date", type date}, {"Hours", Int64.Type}, {"Type", type text}}),

    #"Grouped Rows" = Table.Group(#"Changed Type1", {"Date", "City", "Tech"}, 
                      {{"Max", 
                      each
                        List.Max(Table.AddColumn(_,"MaxHours", each if [Type]="Travel" then [Hours] else null)[MaxHours]),
                      type number}},
                      {{"Min", 
                      each
                        List.Min(Table.AddColumn(_,"MinHours", each if [Type]="Work" then [Hours] else null)[MinHours]),
                      type number}})
in
    #"Grouped Rows"

getting error as

 

Expression.Error: We cannot convert a value of type List to type Number.
Details:
    Value=List
    Type=Type

@MarcelBeug Can you help me.

Anonymous
Not applicable

City, Tech, Time, Work can all be there own dimension table where hours worked is the measure being explored through different dimensions. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.