Forum Discussion
Trying to create a new column based on the query or source file name.
Hi Guys,
I am trying to create a simple new column in my dataset. But I need the data to be the Query name.
What is the formula for Query Name? Or Source File name? Thanks in advance.
Hi dmarics28 ,
Try to change the code of your Append query in the Advanced Editor.
let Source = Table.Combine({Table.AddColumn(#"query A", "source query", each "query A") , Table.AddColumn(#"query B", "source query", each "query B") , Table.AddColumn(#"query C", "source query", each "query C")}) in SourceBTW, .pbix file attached.
Best Regards,
Icey
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
17 Replies
- IceyCommunity Support
Hi dmarics28 ,
Try to change the code of your Append query in the Advanced Editor.
let Source = Table.Combine({Table.AddColumn(#"query A", "source query", each "query A") , Table.AddColumn(#"query B", "source query", each "query B") , Table.AddColumn(#"query C", "source query", each "query C")}) in SourceBTW, .pbix file attached.
Best Regards,
Icey
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- dmarics28Frequent Visitor
Icey
Thanks that did work.
However, i have about 20 or 30 data sources, and typing each one is very time consuming.And need to edit the code each time i need to add another source.
If somehow there was a formula instead on manually typing source name in the advanced editor, that would help.
But thanks to everyone who put some ideas forward on this.
- BA_PeteSuper User
Hi dmarics28 ,
1) In Power Query, go to New Source>Blank Query>Advanced Editor and paste this code over the default code:
let Source = #sections, Section1 = Source[Section1], convertTable = Record.ToTable(Section1) in convertTableThis will load a table with all components of your Power Query setup.
2) Filter the [Name] column to include only the names of the tables that you want to append. Once you have done this, you will see that the dropdown button in the column header changes to an expand button:
3) Expand this as you would after a Merge/Join including all columns that are the same in both queries:
4) You will then have an appended table which includes the source query name in each:
Pete
- dmarics28Frequent Visitor
- BA_PeteSuper User
Yes, unfortunately #sections and #shared are Power Query environment only. I should have checked your use-case first.
In terms of carrying the appended query names over, on balance of time/effort, I see your options as follows:
1) Manually add a column into each of your PRE-APPENDED tables, like ="myTableName1", ="myTableName2" etc. Ensure these columns have the same field name and data type in each table and the append process will bring them in correctly the same as all the other data columns.
2) Use your data source to create a 'Source Name' table that is bundled with the main data table. For example, in Excel you could create another tab that just contains the name that you want the source to be called. Bring this table in with the main data and then use it as a source to reference in an added column in Power Query. In reality, this essentially achieves the same as option 1), but in a far more roundabout way.
Pete
- Hnd12000Frequent Visitor
For me, this does perfectly the job. Thank you
- mahoneypatMicrosoft Employee
You can just put = Query1 (the name of your query). This will add the entire query into a table on each row. Is that your desired outcome?
If this works for you, please mark it as the solution. Kudos are appreciated too. Please let me know if not.
Regards,
Pat
- dmarics28Frequent Visitor
No, That didnt work.
That returned an error.Expression.Error: The name 'query1' wasn't recognized. Make sure it's spelled correctly.
- ziying35Impactful Individual
Do you want to get a list of all query names for the current file in a new column? Or a list collection that references all queries?
- ziying35Impactful Individual
the query ribbon → Add Column → Custom Column →
Text.Combine(Record.FieldNames(Record.RemoveFields(#sections[Section1], "Append1")),", ")Append1 is the name of the current query, Since Sections also display the name of the current query, the name of the current query should be removed
- ziying35Impactful Individual
// out let Source = Record.ToTable(Record.RemoveFields(#sections[Section1],"out")), expd = Table.ExpandTableColumn(Source, "Value", {"Value"}, {"Value.1"}) in expd // first let Source = Table.FromValue({1..5}) in Source // second let Source = Table.FromValue({1..5}) in SourceHere are three queries combined for a demonstration to see if this is what you want, That's as far as I can go