如何用Power Query M代码为多公司NAV数据添加公司标识自定义列?
Absolutely! You can easily add that custom "Company" column with a fixed identifier using Power Query M. Here's how to modify your existing code to achieve the exact output you're looking for:
Modified Code with Company Column
let Source = Sql.Databases("xx.x.x.x"), zzz_LIVE = Source{[Name="NAVzzz_LIVE"]}[Data], #"COMPANY1$G_L Entry" = zzz_LIVE{[Schema="dbo",Item="COMPANY1$G_L Entry"]}[Data], // Add custom column with fixed company name #"Added Company Column" = Table.AddColumn(#"COMPANY1$G_L Entry", "Company", each "Company1") in #"Added Company Column"
Breakdown of the Change
- The
Table.AddColumnfunction is the key here:- First parameter: The target table you want to modify (
#"COMPANY1$G_L Entry") - Second parameter: The name of your new column (
"Company") - Third parameter: A function that returns the value for each row — since this is a fixed identifier for the entire table, we just return the static string
"Company1"
- First parameter: The target table you want to modify (
Bonus: Optimize for Multiple Companies
If you plan to pull data from multiple NAV companies later, you can make the code more reusable by defining the company name as a variable. This way you only need to update one value when switching companies:
let // Define company name as a variable for easy editing TargetCompany = "Company1", Source = Sql.Databases("xx.x.x.x"), zzz_LIVE = Source{[Name="NAVzzz_LIVE"]}[Data], // Use the variable to dynamically reference the table #"G_L Entry Table" = zzz_LIVE{[Schema="dbo",Item=TargetCompany & "$G_L Entry"]}[Data], // Reuse the variable for the company column value #"Added Company Column" = Table.AddColumn(#"G_L Entry Table", "Company", each TargetCompany) in #"Added Company Column"
This will output exactly the table structure you showed, with the "Company" column populated with "Company1" for every row.
内容的提问来源于stack exchange,提问作者Adni
相关产品推荐
相关产品推荐

