Forum Discussion
M Script Help - custom column referencing another table
- 8 years ago
You can add a column in sales with this formula:
Table.SelectRows(Department,
(Dept) => (Dept[FromDate] < [Invoice Date]) and
(Dept[ToDate] >= [Invoice Date]) and
(Dept[Seller] = [Seller])
)[Department]{0}This will lookup the desired value from te Department-table (and if it is null, replace it with the value of your current row).
But it might be slow. To speed it up you can "partition" your Sales table by grouping it on Seller, merge with Department on seller and apply the above selection-formula in the partitioned fields: Just omit the "(Dept[Seller] = [Seller])"-part then.
Another alternative is to create an intermediate table that you dont load to the data model where you expand the dates of the time intervals of your Department-table so that every day will have one row. Then you can simply merge that new date-column with your Sales-table.
Invoking ImkeF
- ImkeF8 years ago
Community Champion
You can add a column in sales with this formula:
Table.SelectRows(Department,
(Dept) => (Dept[FromDate] < [Invoice Date]) and
(Dept[ToDate] >= [Invoice Date]) and
(Dept[Seller] = [Seller])
)[Department]{0}This will lookup the desired value from te Department-table (and if it is null, replace it with the value of your current row).
But it might be slow. To speed it up you can "partition" your Sales table by grouping it on Seller, merge with Department on seller and apply the above selection-formula in the partitioned fields: Just omit the "(Dept[Seller] = [Seller])"-part then.
Another alternative is to create an intermediate table that you dont load to the data model where you expand the dates of the time intervals of your Department-table so that every day will have one row. Then you can simply merge that new date-column with your Sales-table.
- ImkeF8 years ago
Community Champion
and a link to an article I've written about "partitioning": https://wp.me/p6lgsG-yh
- Anonymous8 years agoNot applicable
ImkeF Thank you for the solution! I have tried it on the sample dataset, and I get an error for the rows where i am expecting it to be replaced with the current row (value in DepartmentDefault).
- ImkeF8 years ago
Community Champion
You'll probably get the errors are probably where there is no match with the other table?
Either replace it with the value from the other column or write a conditional statement that uses that column if the other operatoin failed.
- edhans6 years ago
Community Champion
ImkeF why is this necessary?
ImkeF wrote:
You can add a column in sales with this formula:
Table.SelectRows(Department,
(Dept) => (Dept[FromDate] < [Invoice Date]) and
(Dept[ToDate] >= [Invoice Date]) and
(Dept[Seller] = [Seller])
)[Department]{0}This will lookup the desired value from te Department-table (and if it is null, replace it with the value of your current row).
In other words, what exactly is (Dept) => doing here?
This was super helpful for me, but not 100% sure what is going on. I cannot figure out why I cannot simply use something like
=Table.AddColumn(#"Changed Type", "Other", each Table.SelectRows(OtherTable, each [OtherTableColumn] = 1))I can do this with other steps in the current query, using something like this:
Table.AddColumn(#"Changed Type", "Other", each Table.SelectRows(#"Changed Type", each [SomeColumn] = 1))But obviously the table I'm referencing in my first example (OtherTable) isn't the same as a table in another step (#"Changed Type").
Your code works beautifully, but not tracking it...