The scenario and data structure is very simple.
I have a list with the product code and the month that this product have been retailed. A example of such data can be seen at the first two columns in green at the image below.
Then I need to check for each product If it was retailed also on the last month, on the last 3 months or in the last 12 months. The result would be the next three columns in yellow on the image.
These calculations (yellow columns) are easy to be computed at Excel by using some IF and COUNTIFS formulas, but when migrating it to Power BI I'm struggling with the performance of my code at Power Query. As there are thousands of products for each month, the Power Query calculation is taking too long.
Check below the code I've designed. The code snapshot would be for the second yellow column, to advise whether there was a retail on the last 3 months or not of that product.
In essence what I'm doing is adding a calculated column that is counting the rows of a table that is being filtered with the product code information and relevant date.
What would be a better approach in terms of performance to get the information I need?
Thank you.
Code:
// Add a calculated column.
AdicionarHits03Meses = Table.AddColumn(
AdicionarHits01Mes,
"Hit nos últimos 3 meses?",
(r)=>
// Check if...
if
// Returns the rows count of a table.
Table.RowCount(
// Returns a table with the condition.
Table.SelectRows(
ChangeType,
// Condition:
(q)=>
// Same Product Code.
q[#"Product Code"] = r[#"Product Code"]
and
// Check the retail month.
q[#"Retail month"] <= Date.AddMonths(r[#"Retail month"], -1) and
q[#"Retail month"] >= Date.AddMonths(r[#"Retail month"], -3)
)
)
= 0 then
// No retail found.
0 else
// Retail found.
1
,
Int64.Type
)
Table.SelectRows
within yourTable.AddColumn
function? Why not just useeach
within theTable.AddColumn
function to evaluate the expression in line with your rows? Your function has no actual outer references? Given your desired output, I don't see why that is required? For example, I don't see why ther
function could not just beeach if [#"Retail month"] <= Date.AddMonths([#"Retail month"], -1) and [#"Retail month"] >= Date.AddMonths([#"Retail month"], -3) then 1 else 0
? – trenton-ftw