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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
gracitg8
New Member

Create a custom column in Powery Query filtering about another column

Hello everyone! 

I have a problem in power query that I don't know how to translate into M code, I hope someone can help me with it.

I have the following table, where the month and category are defined. I need the code to generate my Result table, where it has to return the value of the month if all the categories are presented and if not a null value. I have tried several formulas but nothing works for me, any idea how to get it?

thank you so much ! Greetings to all

gracitg8_0-1704570145359.png

ps: sorry for my categories in Spanish

1 ACCEPTED SOLUTION
ronrsnfld
Super User
Super User

Table.Group is useful here. Add the column after you have grouped by month; then re-expand the table:

 

let

//My source is an Excel table but can be anything
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"mes", Int64.Type}, {"Categoría", type text}}),

//create list of all the categories
    #"All Categoría" = List.Distinct(#"Changed Type"[Categoría]),

//group by "mes", then add the column in a custom aggregation
    #"Grouped Rows" = Table.Group(#"Changed Type", {"mes"}, {
        {"All Cat present", (t)=>Table.AddColumn(t,"Resultado", 
            each if List.ContainsAll(t[Categoría], #"All Categoría") then t[mes]{0} else null, Int64.Type), 
            type table [mes=nullable number, Categoría=nullable text, Resultado=Int64.Type]}}),

//Re-expand the collapsed table
    #"Expanded All Cat present" = Table.ExpandTableColumn(#"Grouped Rows", "All Cat present", {"Categoría", "Resultado"})
in
    #"Expanded All Cat present"

 Data

ronrsnfld_0-1704577172191.png

 

Results

ronrsnfld_1-1704577200566.png

 

 

View solution in original post

1 REPLY 1
ronrsnfld
Super User
Super User

Table.Group is useful here. Add the column after you have grouped by month; then re-expand the table:

 

let

//My source is an Excel table but can be anything
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"mes", Int64.Type}, {"Categoría", type text}}),

//create list of all the categories
    #"All Categoría" = List.Distinct(#"Changed Type"[Categoría]),

//group by "mes", then add the column in a custom aggregation
    #"Grouped Rows" = Table.Group(#"Changed Type", {"mes"}, {
        {"All Cat present", (t)=>Table.AddColumn(t,"Resultado", 
            each if List.ContainsAll(t[Categoría], #"All Categoría") then t[mes]{0} else null, Int64.Type), 
            type table [mes=nullable number, Categoría=nullable text, Resultado=Int64.Type]}}),

//Re-expand the collapsed table
    #"Expanded All Cat present" = Table.ExpandTableColumn(#"Grouped Rows", "All Cat present", {"Categoría", "Resultado"})
in
    #"Expanded All Cat present"

 Data

ronrsnfld_0-1704577172191.png

 

Results

ronrsnfld_1-1704577200566.png

 

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.