Forum Discussion

saqwild's avatar
saqwild
Frequent Visitor
1 year ago

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

  • 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!

    • saqwild's avatar
      saqwild
      Frequent Visitor

      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

    • saqwild's avatar
      saqwild
      Frequent Visitor

      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.

  • 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

     

    • saqwild's avatar
      saqwild
      Frequent Visitor

      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.

      • gmsamborn's avatar
        gmsamborn
        Super User

        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.