Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
VoltesDev
Helper IV
Helper IV

Need help on classifying value

Hello guys,

 

Need you help on my issue here, that if I have this kind of data :

VoltesDev_0-1639734031552.png

What I need is to calculate the age of the ending qty. 

 

I have a setup of grouping to classify the age like this :

VoltesDev_0-1639736302115.png

 

 

How to make a matrix that grouping the end quantity ?

The problem I noticed is I can't just use the Date and using Datediff, if lets say today is April 2021. because it will need to track when it is minus, it should substract the oldest qty first. This is actually for inventory movement transactions which usually we called it First In First Out (FIFO)

 

This is what I'm talking about:

If just by Sum with its respective date (with Datediff to April 2021), I will get like this :

VoltesDev_1-1639736332390.png

 

But that is wrong, the correct result should be like this :

VoltesDev_2-1639736361324.png

 

Both ending (available) qty is same, 12, but we cannot just sum the month of March to be -3, since the minus is supposed to substrat the previous qty which maybe not in the same month.

 

I wonder this kind of analysis / calculation can be achieve by DAX ? FYI, the data will also have different items (ItemsA, itemsB, etc) I just want to describe more clear so only use 1 item.

 

Please advice.

Thanks

 

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION
v-yalanwu-msft
Community Support
Community Support

Hi, @VoltesDev ;

Try it.

Measure = 
var _1m=CALCULATE(SUM([Qty]),FILTER(ALL('Table'),[Item]=MAX('Table'[Item])&&[month]="1 month"))
var _2m=CALCULATE(SUM([Qty]),FILTER(ALL('Table'),[Item]=MAX('Table'[Item])&&[month]="2 month"))
var _3m=CALCULATE(SUM([Qty]),FILTER(ALL('Table'),[Item]=MAX('Table'[Item])&&[month]=">2 month"))
var   _total=SWITCH( MAX([month]),"1 month",IF(_1m+_2m> 0&&_1m<0,_1m+_2m,0),"2 month",IF(_1m+_2m<0,_1m+_2m+_3m,_2m),">2 month",IF(_1m<0&&_1m+_2m>0,_3m-_2m,0))
return IF(HASONEVALUE('Table'[month]),_total,SUM('Table'[Qty]))

The final output is shown below:

vyalanwumsft_0-1640053670576.png

Best Regards,
Community Support Team_ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
v-yalanwu-msft
Community Support
Community Support

Hi, @VoltesDev ;

Try it.

Measure = 
var _1m=CALCULATE(SUM([Qty]),FILTER(ALL('Table'),[Item]=MAX('Table'[Item])&&[month]="1 month"))
var _2m=CALCULATE(SUM([Qty]),FILTER(ALL('Table'),[Item]=MAX('Table'[Item])&&[month]="2 month"))
var _3m=CALCULATE(SUM([Qty]),FILTER(ALL('Table'),[Item]=MAX('Table'[Item])&&[month]=">2 month"))
var   _total=SWITCH( MAX([month]),"1 month",IF(_1m+_2m> 0&&_1m<0,_1m+_2m,0),"2 month",IF(_1m+_2m<0,_1m+_2m+_3m,_2m),">2 month",IF(_1m<0&&_1m+_2m>0,_3m-_2m,0))
return IF(HASONEVALUE('Table'[month]),_total,SUM('Table'[Qty]))

The final output is shown below:

vyalanwumsft_0-1640053670576.png

Best Regards,
Community Support Team_ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

AlexisOlson
Super User
Super User

I haven't tested this logic carefully, but I think something like this might get you on the right track:

2 month =
VAR MaxDate   = CALCULATE ( MAX ( Inventory[Date] ), ALL ( Inventory ) )
VAR StartDate = MaxDate - 60 //Days To
VAR EndDate   = MaxDate - 30 //Days From
VAR PriorQty =
    CALCULATE ( SUM ( Inventory[Qty] ), Inventory[Date] <= StartDate )
VAR AddedQty =
    CALCULATE (
        SUM ( Inventory[Qty] ),
        Inventory[Date] > StartDate,
        Inventory[Date] <= EndDate,
        Inventory[Qty] > 0
    )
VAR SubtractedQty =
    - CALCULATE (
        SUM ( Inventory[Qty] ),
        Inventory[Date] > StartDate,
        Inventory[Qty] < 0
    )
RETURN
    IF (
        PriorQty > SubtractedQty,
        AddedQty,
        PriorQty + AddedQty - SubtractedQty
    )
smpa01
Super User
Super User

@VoltesDev  can you please provide sample data and desired output in table format?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Hi @smpa01 ,

 

I'm sorry, I hope this is applicable ?

ItemDateQty

ItemA

9/1/202110
ItemA10/1/20215
ItemA11/1/20212
ItemA11/1/2021-5
ItemB10/1/20213
ItemC9/1/20214
ItemC10/1/20213
ItemC11/1/2021-5

 

And the grouping :

AgeFromTo
1 month030
2 month3160
> 2 month619999

 

And the expectation is like this :

VoltesDev_0-1639754106594.png

 

Many thanks in advance.

Hi @smpa01 ,

 

So sorry but I seems cannot get the table right, tried many ways, from copying from excel, insert table from this tools and copy from excel again until I tried to insert table again and key in manually one by one, but it looks terrible after post.

 

But if I tried to copy back, it is already in table format. I'm not sure what's going on with this editor.

 

Thanks

amitchandak
Super User
Super User

@VoltesDev , If this based on today's date you can add a new column

 

New Column =

var _diff = datediff([Date], today(), Month())

return

Switch( True() ,

_diff <= 1 , " 1 Month",

_diff <= 2 , " 2 Month",

"> 2 Month"

)

 

Use this in visual

 

 

In case you need based on the selected date, You need a measure and then you need to use segmentation

Dynamic Segmentation Bucketing Binning
https://community.powerbi.com/t5/Quick-Measures-Gallery/Dynamic-Segmentation-Bucketing-Binning/m-p/1...


Dynamic Segmentation, Bucketing or Binning: https://youtu.be/CuczXPj0N-k

Hi @amitchandak ,

 

I'm sorry, but I believe that only will not work. If I only use the date and calculate DateDiff, it will make the 1st month column -3 like my example. So the expectation is the 2nd matrix like this :

VoltesDev_0-1639752766543.png

 

Thanks,

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors