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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

Dynamically update column names

I've got a series of tables that all look the same for different countries, I.e.:

 

DateSessionsUsersGoal 1Goal 2Goal 3etc
04/06/1935925232213etc
etc

 

I've then got another table with a set of standardised labels that I want to replace with their respective column names in their individual country tables. The issue is that the column names don't match up with each country's table and changing it is not an option. You can see the issue in the column name table here:

 

CountryGoalLabel
USGoal 1Blue
USGoal 2Red
USGoal 3Yellow
GBGoal 1Red
GBGoal 2Yellow
GBGoal 3Green
AUGoal 1Green
AUGoal 2Blue
USGoal 3Red

 

How would I get it so that each label is dynamically applied as the column name for each country's table and the goal's respective column?

 

Any help in getting this up and running for a Power Query & M newbie is much appreciated.

1 ACCEPTED SOLUTION
v-yulgu-msft
Employee
Employee

Hi @Anonymous ,

 

You need to unpivot each country table, then, add a custom column to lookup to [Label] field from the summary table. At last, pivot country table.

 

To reference to column in another table, please see below M function.

 #"Added Custom" = Table.AddColumn(#"Renamed Columns", "Custom", each (let currentGoal = [Goal] in Table.SelectRows(TB1, each [Country] = "GB" and [Goal] = currentGoal)){0}[Label]),

 

The whole M code in advanced editor is like below.

let
    Source = Excel.Workbook(File.Contents("C:\Users\xxxxx\Desktop\Sample Data.xlsx"), null, true),
    GB_Sheet = Source{[Item="GB",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(GB_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Sessions", Int64.Type}, {"Users", Int64.Type}, {"Goal 1", Int64.Type}, {"Goal 2", Int64.Type}, {"Goal 3", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date", "Sessions", "Users"}, "Attribute", "Value"),
    #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Columns",{{"Attribute", "Goal"}}),
    #"Added Custom" = Table.AddColumn(#"Renamed Columns", "Custom", each (let currentGoal = [Goal] in Table.SelectRows(TB1, each [Country] = "GB" and [Goal] = currentGoal)){0}[Label]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Goal"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Custom]), "Custom", "Value")
in
    #"Pivoted Column"

 1.PNG

 

I have uploaded the sample .pbix file for your reference.

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
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

6 REPLIES 6
v-yulgu-msft
Employee
Employee

Hi @Anonymous ,

 

You need to unpivot each country table, then, add a custom column to lookup to [Label] field from the summary table. At last, pivot country table.

 

To reference to column in another table, please see below M function.

 #"Added Custom" = Table.AddColumn(#"Renamed Columns", "Custom", each (let currentGoal = [Goal] in Table.SelectRows(TB1, each [Country] = "GB" and [Goal] = currentGoal)){0}[Label]),

 

The whole M code in advanced editor is like below.

let
    Source = Excel.Workbook(File.Contents("C:\Users\xxxxx\Desktop\Sample Data.xlsx"), null, true),
    GB_Sheet = Source{[Item="GB",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(GB_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Sessions", Int64.Type}, {"Users", Int64.Type}, {"Goal 1", Int64.Type}, {"Goal 2", Int64.Type}, {"Goal 3", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date", "Sessions", "Users"}, "Attribute", "Value"),
    #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Columns",{{"Attribute", "Goal"}}),
    #"Added Custom" = Table.AddColumn(#"Renamed Columns", "Custom", each (let currentGoal = [Goal] in Table.SelectRows(TB1, each [Country] = "GB" and [Goal] = currentGoal)){0}[Label]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Goal"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Custom]), "Custom", "Value")
in
    #"Pivoted Column"

 1.PNG

 

I have uploaded the sample .pbix file for your reference.

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Awesome, thanks for that, @v-yulgu-msft . That worked!

wintee
New Member

You should unpivot your 'country' tables in order to create a table that can be joined to the other table.

From there you can pivot this new table.

Anonymous
Not applicable

Thanks, @wintee for your reply.

 

I've tried that, but the moment that I repivot the table so I can then get each of the goals as a column, the relationship between label mapping breaks. 

 

What am I missing here?

edhans
Super User
Super User

Can you post an example of what the expected output is? I cannot figure out how those two tables are related.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Anonymous
Not applicable

Sure thing, I am expecting this output for each country table:

 

US

DateSessionsUsersBlueRedYellowetc
04/06/1935925232213etc
etc

GB

DateSessionsUsersRedYellowGreenetc
04/06/193414531163etc
etc

AU

DateSessionsUsersGreenBlueRedetc
04/06/194217512141etc
etc

 

And so on. 

 

Does this make sense?

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors
Top Kudoed Authors