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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
saqwild
Frequent Visitor

Dynamic calculation based on helper table and selected period

Hi,

I have seen many posts and video related to my question, however nothing seems to be working as I expected, though I have tried several option and combinations. Hence seeking help herewith.

Scenario:

I have three tables as below.

1. Extract table, which contains my data, customer code, name, transaction reference, invoice data and amount

Account Code    Account Name    Trans Date   Trans Ref   Amount  
150011ABC Co20/06/202424345786389
150011ABC Co23/07/202424345798876
150012XYZ Co23/01/202121663525566
150012XYZ Co15/04/202222266787744

 

2. Grid Table, containing matrix to calculate the amounts within threshold.

order  group   min  max      Percent
10-600600%
261-9061905%
391-1809118010%
4181-36018136050%
5360+36199999999100%

 

3. Selection Period Table, to show current or forecast calculation

Selected Period    Period Status    
31/07/2024Current
31/08/2024Current +30
20/09/2024Current +90

 

Above table I have generated with MAX of Transaction Date from extract table + EDATE(+x)

 

 

PeriodTable = 
        VAR BDLastDate = FORMAT(max('Extract'[Transaction Date]),"dd/mm/yyyy")
        VAR BDFCT30 = EDATE(BDLastDate,1)
        VAR BDFCT60 = EDATE(BDLastDate,2)
        VAR BDFCT90 = EDATE(BDLastDate,3)
RETURN
{(BDLastDate,"Current"),(BDFCT30,"Current +30"),(BDFCT60,"Current +60"),(BDFCT90,"Current +90")}

 

 

I have renamed the columns to "SelectedPeriod" and "Period Status". "Period Status" is my slicer filter 

 

Requirement:

I want to achieve the following.

  • generate invoice age based on slicer filter, if slicer filter is "Current" then based on last date of transaction period, "Current +30" would be last transaction date +30 days and so on (as above)
  • generate calculated amount within each Grid group, what will fall under 0-30, 0-60,0-90 and so on. This will change if the slicer filter changed from one to another. i.e. if slicer changes from "Current" to "Current +30", accordingly the grid, age and calculation will change.
  • generate allowance provision based of Grid min max group divided by percent. i.e. if an amount is withing 0-60 days group then it will calculate a provision of 0%, 61-90 days will be amount multiplied by 5% and so on. This also needs to be changed as and when the slicer filter changes.

Trust above clarify the scenario.

Thank you in advance for the time and effort.

8 REPLIES 8
gmsamborn
Super User
Super User

Hi @saqwild 

 

Would the following measures help?

 

Value = SUM( 'Extract'[Amount] )


Invoice Age = 
VAR _SelectedPeriod = SELECTEDVALUE( 'Selection Period Table'[Selected Period] )
VAR _InvDt = MAX( 'Extract'[Trans Date] )
VAR _Result =
    DATEDIFF(
        _InvDt,
        _SelectedPeriod,
        DAY
    )
RETURN
    IF(
        HASONEVALUE( 'Extract'[Trans Ref] ),
        _Result,
        ""
    )


Aging Group = 
VAR _Age = [Invoice Age]
VAR _Result =
    CALCULATE(
        MAX( 'Buckets'[group] ),
        'Buckets'[min] <= _Age
            && 'Buckets'[max] >= _Age
    )
RETURN
    IF(
        HASONEVALUE( 'Extract'[Trans Ref] ),
        _Result,
        ""
    )


Pct = 
VAR _Age = [Invoice Age]
VAR _Result =
    CALCULATE(
        MAX( 'Buckets'[Percent] ),
        'Buckets'[min] <= _Age
            && 'Buckets'[max] >= _Age
    )
RETURN
    IF(
        HASONEVALUE( 'Extract'[Trans Ref] ),
        _Result,
        ""
    )


New Value = 
    SUMX(
        VALUES( 'Extract'[Trans Ref] ),
        [Value] * ( 1 + [Pct] )
    )

 

 

Let me know if you  have any questions.

 

saqwild.pbix

 

@gmsamborn thank for your reply.

as i mentioned previsouly, my selected period is not the date but status

VAR _SelectedPeriod = SELECTEDVALUE( 'Selection Period Table'[Selected Period] )
this would be 
VAR _SelectedPeriod = SELECTEDVALUE( 'Selection Period Table'[Period Status] )

 at the same time i want to make the report dynamic based on Period status = Selected period.

I tried the above code an static date, all works fine excep, the date is hardcoded now and the total value of "New Value" is not correct.

Hi @saqwild 

 

If both are in the same table, DAX can figure this out if you are using SELECTEDVALUE() since it looks at the whole row, not just the field in the slicer.

 

Check my pbix.

thank you @gmsamborn, that works fine,

juse one issue now, when i try values by ageing groups, it always shows 100% bucket

saqwild_0-1726428304257.png

thanks

Any assistance on this is appreciated 

@Ahmedx 

@AlexisOlson 

@amitchandak 

@Ashish_Mathur 

@danextian 

@Fowmy 

Sahir_Maharaj
Super User
Super User

Hello @saqwild,

 

Can you please try the following approach:

 

1. Calculate the difference between the transaction date and the selected period

Invoice Age = 
VAR SelectedPeriod = MAX('Selection Period Table'[Selected Period])  -- Get the selected period from the slicer
RETURN
DATEDIFF('Extract'[Trans Date], SelectedPeriod, DAY)

2. Classify the invoices

Amount By Group = 
VAR InvoiceAge = [Invoice Age]  -- Use the invoice age calculated above
RETURN
CALCULATE(
    SUM('Extract'[Amount]),
    FILTER(
        'Grid Table',
        InvoiceAge >= 'Grid Table'[min] && InvoiceAge <= 'Grid Table'[max]
    )
)

3. Calculate the provision based on the thresholds and percentages

Provision Amount = 
VAR InvoiceAge = [Invoice Age]  -- Use the invoice age calculated earlier
VAR GroupPercent = 
    CALCULATE(
        MAX('Grid Table'[Percent]), 
        FILTER(
            'Grid Table',
            InvoiceAge >= 'Grid Table'[min] && InvoiceAge <= 'Grid Table'[max]
        )
    )
RETURN
[Amount By Group] * GroupPercent

Hope this helps!


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

@Sahir_Maharaj 

I can not refer the column name directly in the measure hence i tried the following 

Invoice Age = 
VAR _CurrentPeriod = EOMONTH(MAX('Extract'[Transaction Date]),0)
VAR _ListDates = SELECTEDVALUE('Extract'[Transaction Date])
VAR _CurrentAge = DATEDIFF(_ListDates, _CurrentPeriod, DAY)

RETURN _CurrentAge

 but its giving the weired numebrs, not matching when i calculate manually.

Thank you Sahir,

As mentiond above, my slicer is not the date but "Period Status", so i cant directly calculate the datedif.

Let me try with a switch and see if it works based on max and edate

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.