Forum Discussion
Classify values in a new column based on previous month value
Hello;
I have the following issue:
I have a fact table with Date, Product, Company, Region and Value, and i need to add a column to classify the products based on the Month to Month or Year to Year variation. So in the end i will have a column with three possible values "Increased", "Decreased" and "No changes".
I tried to do this with a SUMMARIZE table
Summarized_Table = SUMMARIZECOLUMNS(Fact_Table[Product],Fact_Table[Date],"Previous Month",PREVIOUSMONTH(Fact_Table[Date]),"Actual Value",SUM(Value))
and within that table added a calculated column for the previous month value of the product
Previous Value =
var Product = Summarized_Table[Product]
var Date = Summarized_Table[Previous Month]
var PValue = CALCULATE(SUM(Fact_Table[Value]),Fact_Table[Date] = Date && Fact_Table[Product] = Product)
return PValue
and then adding the classify column to the summarized table:
Product_Status =
SWITCH(
TRUE(),
Summarized_Table[Actual Value] > Summarized_Table[Previous Value],"Increased",
Summarized_Table[Actual Value] < Summarized_Table[Previous Value],"Decreased","No changes"
)
Testing the summarized table, it works ok, it shows the correct data.
| Product_Status | #Products | Actual Value | Previous Value |
| Increased | 12 | 200.40 | 190.50 |
| Decreased | 5 | 120.30 | 150.60 |
| No Changes | 1 | 100.00 | 100.00 |
But i need the Product_Status in the Fact Table so i can analyze by company and region.
So what i do is add it the same i did with the summarized table:
Product Status =
var Product = Fact_Table[Product]
var Date = Fact_Table[Date]
var Status = CALCULATE(MAX(Summarized_table[Product_Status]),Summarized_Table[Date] = Date && Summarized_Table[Product] = Product)
return Status
But when i test it in the fact table something weird happen, the "Previous Value" in the Fact Table is wrong:
| Product_Status | #Products | Actual Value | Previous Value |
| Increased | 12 | 200.40 | 150.60 |
| Decreased | 5 | 120.30 | 190.50 |
| No Changes | 1 | 100.00 | 100.00 |
| Total | 420.70 | 441.10 |
The Previous Value formula in the fact table is like this :
| Product_Status | #Products | Actual Value | Previous Value |
| Increased | 12.00 | ||
| Decreased | 1 | 9.00 | |
| 9.00 | 12.00 |
Product A increased it's value, but that was in April22 vs March22 not in May22 so i can't understand why is not correctly assigned.
Anonymous , I would advice dynamic segmentation
Diff = Sum(Table[Value]) - calculate(Sum(Table[Value]) , previousmonth(Date[Date]) )
Create a table with Start, end and bucket name
-9999 , 0, "Decreased"
0,0.00000001, "No Change"
0.00000001 , 99999, "Increased"
Note in case you do not want a range base bucket refer hwo can use a measure with text values, in the last video link
Then you can create measures with help from this bucket table
countx(filter(values(Product[Product]) , [Diff] >= min(bucket[Start]) && [Diff]< Max(Bucket[end]) ),[Product])
you need a bucket column in visual
or try
sumx(Values( bucket[Bucket]) , calculate( countx(filter(values(Product[Product]) , [Diff] >= min(bucket[Start]) && [Diff]< Max(Bucket[end]) ),[Product])) )
refer
Dynamic Segmentation Bucketing Binning
https://community.powerbi.com/t5/Quick-Measures-Gallery/Dynamic-Segmentation-Bucketing-Binning/m-p/1387187#M626
Dynamic Segmentation, Bucketing or Binning: https://youtu.be/CuczXPj0N-ktext bucket - https://www.youtube.com/watch?v=EyL7KMw877Q
2 Replies
- amitchandak
Super User
Anonymous , I would advice dynamic segmentation
Diff = Sum(Table[Value]) - calculate(Sum(Table[Value]) , previousmonth(Date[Date]) )
Create a table with Start, end and bucket name
-9999 , 0, "Decreased"
0,0.00000001, "No Change"
0.00000001 , 99999, "Increased"
Note in case you do not want a range base bucket refer hwo can use a measure with text values, in the last video link
Then you can create measures with help from this bucket table
countx(filter(values(Product[Product]) , [Diff] >= min(bucket[Start]) && [Diff]< Max(Bucket[end]) ),[Product])
you need a bucket column in visual
or try
sumx(Values( bucket[Bucket]) , calculate( countx(filter(values(Product[Product]) , [Diff] >= min(bucket[Start]) && [Diff]< Max(Bucket[end]) ),[Product])) )
refer
Dynamic Segmentation Bucketing Binning
https://community.powerbi.com/t5/Quick-Measures-Gallery/Dynamic-Segmentation-Bucketing-Binning/m-p/1387187#M626
Dynamic Segmentation, Bucketing or Binning: https://youtu.be/CuczXPj0N-ktext bucket - https://www.youtube.com/watch?v=EyL7KMw877Q
- AnonymousNot applicable
This is the solution based on amitchandak post
Value Prev Month Bucket = SUMX(FILTER(VALUES(Product[ProductId]),Measures[Diff Value] >= MIN('Prod Bucket'[Start]) && Measures[Diff Value] < MAX('Prod Bucket'[End])),Measures[Value Prev Month])Thanks a lot.