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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
ccernat
Frequent Visitor

GroupBy M Language - with IF for each row to get Value

Hello, guys!

 

I have the table bellow and I'm proper stucked, as I don't know how to proceed. It would be wonderful a hint to get me going.

 

Capture.JPG

 

First, I need to switch all the Month names in the Attribute column to their corresponding numbers. After, I need to use Table.GroupBy for the first 7 columns and to return me the first non zero value from Value column for each row where Atrribute ( month number ) < Month. 

 

Is it possible such abomination? 🙂 I need a small path to follow, I can expand from there. Thank you very much!

1 ACCEPTED SOLUTION
v-juanli-msft
Community Support
Community Support

Hi @ccernat 

Create such M query

let
    Source = Excel.Workbook(File.Contents("C:\Users\maggiel\Desktop\case\4\4.24\4.24.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"year", Int64.Type}, {"month", Int64.Type}, {"CheieAnLuna", type date}, {"site", type text}, {"Cod Client", Int64.Type}, {"Cod Contract", Int64.Type}, {"Cod Spatiu", Int64.Type}, {"Attribute", type text}, {"Value", Int64.Type}}),
    #"Added Custom Column" = Table.AddColumn(#"Changed Type", "Custom", each if [Attribute] = "January" then 1 else if [Attribute] = "February" then 2 else if [Attribute] = "Mars" then 3 else if [Attribute] = "April" then 4 else if [Attribute] = "Mail" then 5 else if [Attribute] = "June" then 6 else if [Attribute] = "July" then 7 else if [Attribute] = "August" then 8 else if [Attribute] = "Sepember" then 9 else if [Attribute] = "October" then 10 else if [Attribute] = "November" then 11 else if [Attribute] = "December" then 12 else null),
    #"Grouped Rows" = Table.Group(#"Added Custom Column", {"year", "month", "CheieAnLuna", "site", "Cod Client", "Cod Contract", "Cod Spatiu"}, {{"TOREur", each List.First(Table.SelectRows(_, each [month]>[Custom])[Value]), type table [year=number, month=number, CheieAnLuna=date, site=text, Cod Client=number, Cod Contract=number, Cod Spatiu=number, Attribute=text, Value=number, Custom=number, Custom.1=number]}})
in
    #"Grouped Rows"

1.png

 

Best Regards
Maggie

 

Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-juanli-msft
Community Support
Community Support

Hi @ccernat 

Create such M query

let
    Source = Excel.Workbook(File.Contents("C:\Users\maggiel\Desktop\case\4\4.24\4.24.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"year", Int64.Type}, {"month", Int64.Type}, {"CheieAnLuna", type date}, {"site", type text}, {"Cod Client", Int64.Type}, {"Cod Contract", Int64.Type}, {"Cod Spatiu", Int64.Type}, {"Attribute", type text}, {"Value", Int64.Type}}),
    #"Added Custom Column" = Table.AddColumn(#"Changed Type", "Custom", each if [Attribute] = "January" then 1 else if [Attribute] = "February" then 2 else if [Attribute] = "Mars" then 3 else if [Attribute] = "April" then 4 else if [Attribute] = "Mail" then 5 else if [Attribute] = "June" then 6 else if [Attribute] = "July" then 7 else if [Attribute] = "August" then 8 else if [Attribute] = "Sepember" then 9 else if [Attribute] = "October" then 10 else if [Attribute] = "November" then 11 else if [Attribute] = "December" then 12 else null),
    #"Grouped Rows" = Table.Group(#"Added Custom Column", {"year", "month", "CheieAnLuna", "site", "Cod Client", "Cod Contract", "Cod Spatiu"}, {{"TOREur", each List.First(Table.SelectRows(_, each [month]>[Custom])[Value]), type table [year=number, month=number, CheieAnLuna=date, site=text, Cod Client=number, Cod Contract=number, Cod Spatiu=number, Attribute=text, Value=number, Custom=number, Custom.1=number]}})
in
    #"Grouped Rows"

1.png

 

Best Regards
Maggie

 

Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

ccernat
Frequent Visitor

Came up with this:

 

= Table.Group(#"Added Custom", {"Cod Client", "Year", "Month", "Cod Contract", "CheieAnLuna", "Cod Spatiu"}, {{"TOREur", if List.AllTrue([Value]>0, [Attribute]<[Month]) then each List.First([Value]) else [Value]), type number}})

But it says a comma is expected. What am I doing wrong?

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors