Re-using an existing query

Tigerexcel

Active Member
Joined
Mar 6, 2020
Messages
493
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Can you apply an existing query without having to go into the Advanced Editor and amend the "M" programming?
I'll illustrate with an example, the first table has a simple query applied to it:
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Subject", type text}, {"Score", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Subject"}, {{"Count", each [Name]}}),
#"Extracted Values" = Table.TransformColumns(#"Grouped Rows", {"Count", each Text.Combine(List.Transform(_, Text.From), " "), type text})
in
#"Extracted Values"

I receive another table which is in columns H to J, I would like the same output as in columns E to F. I could do it by copying the above query and then manually editing the Table name to whatever the new table is called but I suspect there's a more efficient way of doing this.
Book1
ABCDEFGHIJ
1NameSubjectScoreNew table
2JackMaths84SubjectCountNameSubjectScore
3FredChem73MathsJack Mary Eddie JakeTedMaths20
4PeteEnglish89ChemFred Sally Sara NoelIanEnglish30
5HarryFrench88EnglishPete Don JoelKenMaths40
6MaryMaths60FrenchHarry Ken TomMaryEnglish50
7SallyChem62BartMaths60
8DonEnglish79BertEnglish70
9KenFrench91HerbMaths80
10EddieMaths70
11SaraChem92
12JoelEnglish75
13TomFrench88
14JakeMaths94
15NoelChem95
Sheet1
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
One way I can think of is storing the source table name in a cell that you'd like to change as the source table, reading it in the Power Query into a variable, and use that variable instead of hard coding the table name in M code. This way, you won't need to edit M code.

Say you put the table name in F1, and name the cell as TableName. So, the F1 cell contains the table name and it is a named range called "TableName".
Book1
ABCDEFGHIJ
1NameSubjectScoreSource TableTable2New table
2JackMaths84SubjectCountNameSubjectScore
3FredChem73MathsJack Mary Eddie JakeTedMaths20
4PeteEnglish89ChemFred Sally Sara NoelIanEnglish30
5HarryFrench88EnglishPete Don JoelKenMaths40
6MaryMaths60FrenchHarry Ken TomMaryEnglish50
7SallyChem62BartMaths60
8DonEnglish79BertEnglish70
9KenFrench91HerbMaths80
10EddieMaths70
11SaraChem92
12JoelEnglish75
13TomFrench88
14JakeMaths94
15NoelChem95
Sheet1

And change the M code as below to use the table name from the named range:
Power Query:
let
TableName = Excel.CurrentWorkbook(){[Name="TableName"]}[Content],
Source = Excel.CurrentWorkbook(){[Name=TableName{0}[Column1]]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Subject", type text}, {"Score", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Subject"}, {{"Count", each [Name]}}),
#"Extracted Values" = Table.TransformColumns(#"Grouped Rows", {"Count", each Text.Combine(List.Transform(_, Text.From), " "), type text})
in
#"Extracted Values"
Change "Table2" to "Table3" in the F1 cell and refresh the resulting table.

Just an idea.
 
Upvote 0
Thanks Smozgur, that is clever but still requires some amendment to M code.

I have been playing with the my original file and the closest I can get is to Duplicate query and then go to the where the file is located, select the table which needs to be grouped, it then performs all the steps except the grouping.
 
Upvote 0

Forum statistics

Threads
1,223,761
Messages
6,174,342
Members
452,555
Latest member
colc007

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