Is there a way to create an "If Then" Group By in Power Query using parameter values?

MGTexas123

New Member
Joined
Jan 1, 2020
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I am trying to create a Power Query with a parameter that what would perform the Table.Group function based on the parameter value. For example, if the parameter value Group by Region= "Yes", then sum up the sales and profits for each region. If the parameter value does not equal "Group by Region"="No", do not perform the group function. Here was the M code which produced the error message:


let
GroupByRegion="Yes",
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Profits", Int64.Type}, {"Sales", Int64.Type}}),
#"Conditional Parameter" = Table.AddColumn(#"Changed Type", "Custom", each if GroupByRegion="Yes" then Table.Group({"Sales Region"}, {{"Profits", each List.Sum([Profits]), type number}, {"Sales", each List.Sum([Sales]), type number}}) else 0)
in
#"Conditional Parameter"

When trying to run the query, I get this error message:

Expression.Error: 2 arguments were passed to function which expects between 3 and 5. Details: Pattern= Arguments=List


Any help on this would be greatly appreciated! Thanks!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
where is your "Sales Region"?

btw. I suggest to use [CODE]...[/CODE] tags
 
Upvote 0
Thanks Sandy.

Basically, I am trying to create a Power Query which will sum up certain rows of data based on a condition set by a parameter. Here is the source data:


ProfitsSalesSales Region
1333North
234West
433East
55West
55North
3325East
2833East


If a parameter is set to sum by sales region, I’d like the results to look like this:

Sales RegionProfitsSales
North6338
West289
East91191


If parameter is not set to sum by sales region, the results would look like this:

ProfitsSalesSales Region
1333North
234West
433East
55West
55North
3325East
2833East


Any advice or recommendations would be greatly appreciated.
 
Upvote 0
Cross posted Is there a way to create an "If Then" Group By in Power Query using parameter values?

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Cross posted Is there a way to create an "If Then" Group By in Power Query using parameter values?

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.

Thanks for letting me know. I am new to posting to forums and your suggestion to link to the other posts is much appreciated.
 
Upvote 0
IF (Conditional Column) accept only Value, Column or Parameter
You are trying to add Table

maybe it will help a bit
 
Upvote 0
or maybe something like this:
condition.jpg
 
Last edited:
Upvote 0
Thanks Sandy! You are the best! Very helpful information. I appreciate you taking the time to share it with me.

Best wishes,
Matt
 
Upvote 0
But I don't know you want solution from post#7 or not or you'll try to resolve the problem via post#6.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,957
Messages
6,175,622
Members
452,661
Latest member
Nonhle

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top