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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Strongbuck
Helper I
Helper I

DAX to Calculate a Filtered Set of Form Data - Not in Columns

I'm trying to figure out how to construct a filtered DAX statement to calculate the Percent Recycled field multipled by the Waste Quantity field (Highlighted in Blue).  The data is being captured in a form so the data is not in columns.  I have tried Filter Variables, Filtered SELECTCOLUMNS and Transposing the columns/rows.

 

Each time, I don't get the correct answer.  Can someone please point me in the right direction? 

 

I need the calculation to sum the data by Month and Year so I created two fields as:

 

Recycled_Month = IFERROR (MONTH ('Native Forms [Answer]), BLANK())
Recycled_Year = IFERROR (YEAR ('Native Forms [Answer]), BLANK())

 

Because the Answer field is text, I had to convert the two fields to an Integer.   

 

Covert_Answers to Number = IFERROR(CONVERT('Native Forms'[Answer], INTEGER), BLANK())

 

I only need to report this calculation when the Answer to the Debris Type is "Comingled" or "Comingled Debris" (Highlighted in Yellow).

 

Any help would be appreciated!

 

Form Data.png

1 ACCEPTED SOLUTION

hi @Strongbuck 

 

in the power query editor, create a blank query. Copy and paste the code below into the advanced editor: 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nVRhT4MwEP0rFz7PDNDBPjtMlszFOU2ImftQ4Zw4aEkpRv69xzaBWAeMBML10nf37r2Wzca4NkaGh28yyuC5SJFWvhAhfarniSVpjLCS4hMDBZaxHZ1gTCGId1hjUARxCXXH1tg27ZsypPcUttVYf4OHmYo4U5HglFggV3mwL8DLk7QXDZ9lxOMxZ1xFqqCEZVagQ/gv0tHmnokk4rsYQzjmO5uXJeYsj1E2t7btXqEMaL5fxUqZ3UkFO4RnsZpSvToS6kF9oByCHSasqwl7K5SKMRvPGO8W1W2KeveFshAcX3PTtN0M5oyHRcJ4ryp/xYaroxQH9s1JWkfRZF+iYjEsGWc7TKh+Ly6tV6UbrjkxqflPztKfTs+f8Za2dg2ujFi8gC8x2BMYyIPTHLTqV+fyc98Aaw7cD2CgG+DUBjht/6pGkUttIOj2Bw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Form #" = _t, Question = _t, Answer = _t, Recycled_Month = _t, Recycled_Year = _t, #"Covert_Answers to Numbers" = _t, #"Project Name" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Form #", Int64.Type}, {"Question", type text}, {"Answer", type text}, {"Recycled_Month", Int64.Type}, {"Recycled_Year", Int64.Type}, {"Covert_Answers to Numbers", Int64.Type}, {"Project Name", type text}}),
    
    //Filtered rows to where question contains recycle ,waste or debris
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each Text.Contains([Question], "Recycle") or Text.Contains([Question], "Waste") or Text.Contains([Question], "Debris")),
    
    //grouped rows by form # and project name
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Form #", "Project Name"}, {{"Data", each _ }}),
    
    // filter the grouped table for question and answer columns
    FilteredTable = Table.AddColumn(#"Grouped Rows", "FilteredTable", each Table.SelectColumns( [Data] , {"Question","Answer"} )),
    
    //remove originally grouped data
    #"Removed Columns" = Table.RemoveColumns(FilteredTable,{"Data"}),
    
    //expand the table
    #"Expanded FilteredTable" = Table.ExpandTableColumn(#"Removed Columns", "FilteredTable", {"Question", "Answer"}, {"Question", "Answer"}),
    
    // create a merged anchor column to pivot on
    #"Project Name -- Form #" = Table.AddColumn(#"Expanded FilteredTable", "Project Name -- Form #", each Text.Combine({[Project Name], Text.From([#"Form #"], "en-CA")}, " --"), type text),
    #"Removed Columns1" = Table.RemoveColumns(#"Project Name -- Form #",{"Form #", "Project Name"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns1", List.Distinct(#"Removed Columns1"[Question]), "Question", "Answer"),
    
    // final debris filter condition and answer calculation 
    #"Filtered Debris Type contains Comingle" = Table.SelectRows(#"Pivoted Column", each Text.Contains([Debris Type], "Comingle")),
    #"Changed Type1" = Table.TransformColumnTypes(#"Filtered Debris Type contains Comingle",{{"Debris Type", type text}, {"Date of Recycle", type date}, {"Waste Quantity", type number}, {"Percent Recycled", type number}, {"Percent Recycled - Other", type number}}),
    Answer = Table.AddColumn(#"Changed Type1", "Answer", each [Waste Quantity]*[Percent Recycled])
in
    Answer

 

does the output below meet the requirement?

adudani_0-1722477541844.png

 

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash

View solution in original post

4 REPLIES 4
Strongbuck
Helper I
Helper I

Form #QuestionAnswerRecycled_MonthRecycled_YearCovert_Answers to NumbersProject Name
3Debris TypeWood   Sample Project 1
3Date of Recycle7/1/202472024 Sample Project 1
3Rx DestinationKentucky Dump   Sample Project 1
3Waste Quantity10  10Sample Project 1
6Debris TypeComingled Debris   Sample Project 1
6Hauler    Sample Project 1
6Percent Recycled75  75Sample Project 1
6Rx Destination    Sample Project 1
6Rx Other Destination    Sample Project 1
6Waste Quantity10  10Sample Project 1
7Debris TypeBottles/Cans   Sample Project 1
7HaulerEveryone's Handyman   Sample Project 1
7Percent Recycled - Other100  100Sample Project 1
7Rx DestinationMetal Management   Sample Project 1
7Date of Recycle7/1/2024   Sample Project 1
7Waste Quantity50  50Sample Project 1
88Debris TypeComingled   Sample Project 2
88HaulerKY Wrecking and Recycling   Sample Project 2
88Percent Recycled75  75Sample Project 2
88Rx DestinationLand Recycling   Sample Project 2
88Date of Recycle6/1/202462024 Sample Project 2
88Waste Quantity50  50Sample Project 2
adudani
Super User
Super User

Hello,

Kindly provide the sample input/output in a usable format (excel, csv, table etc.) masking sensitive information.

reference : https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447... 

Thanks,

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash

Hi,

 

I've added the data as a table.  I tried to add the Excel spreadsheet but it displayed an error that that format is not supported.

 

Thanks for your help!

hi @Strongbuck 

 

in the power query editor, create a blank query. Copy and paste the code below into the advanced editor: 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nVRhT4MwEP0rFz7PDNDBPjtMlszFOU2ImftQ4Zw4aEkpRv69xzaBWAeMBML10nf37r2Wzca4NkaGh28yyuC5SJFWvhAhfarniSVpjLCS4hMDBZaxHZ1gTCGId1hjUARxCXXH1tg27ZsypPcUttVYf4OHmYo4U5HglFggV3mwL8DLk7QXDZ9lxOMxZ1xFqqCEZVagQ/gv0tHmnokk4rsYQzjmO5uXJeYsj1E2t7btXqEMaL5fxUqZ3UkFO4RnsZpSvToS6kF9oByCHSasqwl7K5SKMRvPGO8W1W2KeveFshAcX3PTtN0M5oyHRcJ4ryp/xYaroxQH9s1JWkfRZF+iYjEsGWc7TKh+Ly6tV6UbrjkxqflPztKfTs+f8Za2dg2ujFi8gC8x2BMYyIPTHLTqV+fyc98Aaw7cD2CgG+DUBjht/6pGkUttIOj2Bw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Form #" = _t, Question = _t, Answer = _t, Recycled_Month = _t, Recycled_Year = _t, #"Covert_Answers to Numbers" = _t, #"Project Name" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Form #", Int64.Type}, {"Question", type text}, {"Answer", type text}, {"Recycled_Month", Int64.Type}, {"Recycled_Year", Int64.Type}, {"Covert_Answers to Numbers", Int64.Type}, {"Project Name", type text}}),
    
    //Filtered rows to where question contains recycle ,waste or debris
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each Text.Contains([Question], "Recycle") or Text.Contains([Question], "Waste") or Text.Contains([Question], "Debris")),
    
    //grouped rows by form # and project name
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Form #", "Project Name"}, {{"Data", each _ }}),
    
    // filter the grouped table for question and answer columns
    FilteredTable = Table.AddColumn(#"Grouped Rows", "FilteredTable", each Table.SelectColumns( [Data] , {"Question","Answer"} )),
    
    //remove originally grouped data
    #"Removed Columns" = Table.RemoveColumns(FilteredTable,{"Data"}),
    
    //expand the table
    #"Expanded FilteredTable" = Table.ExpandTableColumn(#"Removed Columns", "FilteredTable", {"Question", "Answer"}, {"Question", "Answer"}),
    
    // create a merged anchor column to pivot on
    #"Project Name -- Form #" = Table.AddColumn(#"Expanded FilteredTable", "Project Name -- Form #", each Text.Combine({[Project Name], Text.From([#"Form #"], "en-CA")}, " --"), type text),
    #"Removed Columns1" = Table.RemoveColumns(#"Project Name -- Form #",{"Form #", "Project Name"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns1", List.Distinct(#"Removed Columns1"[Question]), "Question", "Answer"),
    
    // final debris filter condition and answer calculation 
    #"Filtered Debris Type contains Comingle" = Table.SelectRows(#"Pivoted Column", each Text.Contains([Debris Type], "Comingle")),
    #"Changed Type1" = Table.TransformColumnTypes(#"Filtered Debris Type contains Comingle",{{"Debris Type", type text}, {"Date of Recycle", type date}, {"Waste Quantity", type number}, {"Percent Recycled", type number}, {"Percent Recycled - Other", type number}}),
    Answer = Table.AddColumn(#"Changed Type1", "Answer", each [Waste Quantity]*[Percent Recycled])
in
    Answer

 

does the output below meet the requirement?

adudani_0-1722477541844.png

 

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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