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
TK12345
Resolver I
Resolver I

Dynamic buckets based on 2 "date selections"

Hi all,

I need some help with making dynamic buckets based on my selected period and transactiondate. I am calculating my OHW based on my transaction date. I also have some buckets for 30 days 90 days etc. But what I would like to do is to calculate the OHW based on my selected period and transactiondate. 

What I would like to see is:
What was the amount OHW on that selected Period in the bucket <30 days. 30-60 and so on. 

I tried the following code: 

DynamicBucketingMeasure =
VAR MinDate =
CALCULATE(
    MIN(DIM_Calendar[Date]),
    ALL(DIM_Calendar)
)
VAR MaxDate =
  MAX(DIM_Calendar[Date])

Var SelectedPeriod =

CONCATENATE(
    LEFT(
        SELECTEDVALUE(DIM_Calendar[Year-Month]),4),
    RIGHT(
        SELECTEDVALUE(DIM_Calendar[Year-Month]),2)
    )

var Selectedperiodnumber =
VALUE(SelectedPeriod)

VAR Summary =
             SUMMARIZE(
                     FILTER(                  
                     OHW,
                     OHW[TransactionDate] <= MaxDate && OHW[TransactionDate] >= MinDate
                     && OHW[Period] <= Selectedperiodnumber),    
            OHW[Period],
            OHW[Test],
            OHW[TransactionDate],
            OHW[Amount],
            "Bedrag", SUMX(OHW,OHW[Amount]),
            "AantalDagenSelected", SUMX(OHW, DATEDIFF(OHW[TransactionDate], MaxDate, DAY))
)        

VAR Bucketed =
        ADDCOLUMNS (
            Summary,
            "Bucket", SWITCH (
                TRUE (),
                [AantalDagenSelected] > 0 && [AantalDagenSelected] < 30 , "< 30",
                [AantalDagenSelected] >=31 && [AantalDagenSelected] < 60 , "30 - 60",
                [AantalDagenSelected] >=61 && [AantalDagenSelected] < 90 , "60 - 90",
                [AantalDagenSelected] >=91 && [AantalDagenSelected] < 180 , "90 - 180",
                [AantalDagenSelected] >180, "> 180"
            )
        )

 
VAR Result =
 SUMX ( Bucketed, IF ( [Bucket] = SELECTEDVALUE ( Buckets[Bucket] ), [Bedrag],0))
 
Return
Result

I have a test PBIX-file you can find here: Test DynamicBucket.pbix
As well as my expected outcomes. 

Hope someone could help me out.
1 REPLY 1
lbendlin
Super User
Super User

You are mixing up the concepts of days and months - always dangerous. 

 

lbendlin_0-1700406357864.png

Better to use months.

 

Why is the Transaction Date part of the calculations?

 

What is the anchor date for a period? The last day of the month?

 

Attached is my interpretation

 

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.