Forum Discussion
VALORATED CONSUMPTION
- Anonymous7 years ago
While I think DAX can work for something like this, seems like Power Query is better tool for this job.
- Load your data tables, I named them as Purchases and Consumption
- Create two "Setup" Tables:
- There are two: AllMonths and AllArticles
- You can see the applied steps of each, but basically it appended both the data tables and some transformations to ensure we have all the available months and all the available articles
- We then did a full merge of the two setup tables. You will see that I added a custom column in each of the lookup tables of just "1". When doing a full merge based on that, we are really generating the cartesian product. Be sure to that months are sorted correctly (there was a column added for that)
- Then merged that table with the Consumption table to get the "Cost" column, which will generate some nulls ( and this is a good thing! )
- Next, group the rows by Article, and we want all the rows. This will produce a table for each Article with all the available months and cost. If you click in cell (not the actaul text) you will see
- Since the months are sorted correctly, we can fill down in each sub-table. So if there is a null it will pull down the previous value
- Then we can remove all the other columns, except the column with the new table. Expand that table
- Merge that table with the Consumption table to get the "Stock Consumption" figure. There will be nulls and that is ok
- Add a column to multiply those together
- Filter out the nulls and set data type.
Here's your final output that will work when you add in new months and articles:
Much easier to see in PQ and the applied steps. Here is the excel file:
Firstly, click query editor-> Merge Queries as New like below:
You will achieve a table like this:
Then, after close&applied, you can create a calculate column using DAX below:
Unitary Cost =
VAR previous_month =
SWITCH (
Merge1[Month],
"February", "January",
"March", "February",
"April", "March"
)
RETURN
IF (
Merge1[PURCHASES.Cost] <> BLANK (),
Merge1[PURCHASES.Cost],
CALCULATE (
MAX ( PURCHASES[Cost] ),
FILTER (
PURCHASES,
PURCHASES[Month] = previous_month
&& PURCHASES[Article] = Merge1[Article]
)
)
)
Then create another calculate column:
Valorated Consumption = Merge1[Unitary Cost] * Merge1[Stock Consumption]
Community Support Team _ Jimmy Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
this solution is almost complete but in the case that a product "C" has cost values only for January and and April, for the month of March will not bring data because in Febroary there were no.
- Anonymous7 years agoNot applicable
While I think DAX can work for something like this, seems like Power Query is better tool for this job.
- Load your data tables, I named them as Purchases and Consumption
- Create two "Setup" Tables:
- There are two: AllMonths and AllArticles
- You can see the applied steps of each, but basically it appended both the data tables and some transformations to ensure we have all the available months and all the available articles
- We then did a full merge of the two setup tables. You will see that I added a custom column in each of the lookup tables of just "1". When doing a full merge based on that, we are really generating the cartesian product. Be sure to that months are sorted correctly (there was a column added for that)
- Then merged that table with the Consumption table to get the "Cost" column, which will generate some nulls ( and this is a good thing! )
- Next, group the rows by Article, and we want all the rows. This will produce a table for each Article with all the available months and cost. If you click in cell (not the actaul text) you will see
- Since the months are sorted correctly, we can fill down in each sub-table. So if there is a null it will pull down the previous value
- Then we can remove all the other columns, except the column with the new table. Expand that table
- Merge that table with the Consumption table to get the "Stock Consumption" figure. There will be nulls and that is ok
- Add a column to multiply those together
- Filter out the nulls and set data type.
Here's your final output that will work when you add in new months and articles:
Much easier to see in PQ and the applied steps. Here is the excel file: