Forum Discussion

apatwal's avatar
apatwal
Helper III
4 years ago
Solved

Based on Invoice Date filter, create two separate columns

Hi,

 

I need a help in creating below two columns using DAX

I have a calculated column for "Net Change in Cost $", and using this column  I have to create below two columns

1. Net Change in Cost $ - Full Date

2. Net Change in Cost $ - Last 4 Weeks

 

These two columns are based on Invoice Date visual filter

1. Net Change in Cost $ - Full Date

In this we have to use all transactions that fall within date range specified by Invoice Date filter and sum Net Change in Cost $ column.

Eg -  if Invoice Date filter is set to 1/2/2021 ~ 11/30/2021, use all 2021 transactions

 

2. Net Change in Cost $ - Last 4 Weeks

In this we have to use transactions with Invoice Date within the 4 weeks leading up to end date which is specified by Invoice Date filter.

Eg  if Invoice Date filter is set to 5/1/2021 ~ 12/24/2021, the end date is 12/31/2021 so include transactions with Invoice Date between 11/26/2021 and 12/24/2021

 

Also, if Invoice Date filter is set to 4 weeks, then both these columns should have same value.

 

Any help on this will be appreciated.

  • Dear apatwal 

    Columns do not have access to the filter context and hence cannot read any filter. However, you can achieve the same by creating two measures as the values of a matrix visual sliced by transaction number or any other column.
    Supposing that the date filter will come only from the slicer (meaning it is not part of the visual) then the first measure can be:

     

    Net Change in Cost $ - Full Date =
    CALCULATE (
    	SUM ( 'Table'[Net Change in Cost $] ),
    	ALLSELECTED ( 'Table'[Invoice Date] )
    )

     

     For the 2nd measure try

     

    Net Change in Cost $ - Last 4 Weeks =
    VAR EndtDate = MAX ( 'Table'[Invoice Date] )
    VAR StartDate = EndtDate - 28
    VAR Result =
        CALCULATE (
            SUM ( 'Table'[Net Change in Cost $] ),
            FILTER (
                'Table',
                'Table'[Invoice Date] < EndtDate
                    && 'Table'[Invoice Date] >= StartDate
            )
        )
    RETURN
        Result

     

    Please let me know if works. If so kindly mark my reply as accepted solution. Thank you!

8 Replies

  • tamerj1's avatar
    tamerj1
    Community Champion

    Dear apatwal 

    Columns do not have access to the filter context and hence cannot read any filter. However, you can achieve the same by creating two measures as the values of a matrix visual sliced by transaction number or any other column.
    Supposing that the date filter will come only from the slicer (meaning it is not part of the visual) then the first measure can be:

     

    Net Change in Cost $ - Full Date =
    CALCULATE (
    	SUM ( 'Table'[Net Change in Cost $] ),
    	ALLSELECTED ( 'Table'[Invoice Date] )
    )

     

     For the 2nd measure try

     

    Net Change in Cost $ - Last 4 Weeks =
    VAR EndtDate = MAX ( 'Table'[Invoice Date] )
    VAR StartDate = EndtDate - 28
    VAR Result =
        CALCULATE (
            SUM ( 'Table'[Net Change in Cost $] ),
            FILTER (
                'Table',
                'Table'[Invoice Date] < EndtDate
                    && 'Table'[Invoice Date] >= StartDate
            )
        )
    RETURN
        Result

     

    Please let me know if works. If so kindly mark my reply as accepted solution. Thank you!

    • apatwal's avatar
      apatwal
      Helper III

      Hi tamerj1 

        

      Thanks for your reply!

       

      I have created below two DAX measure:

       

      1st Measure:

       

      Net Change in Cost $ - Full Date Range =

      var startdate = MIN('table'[Date])

      var enddate= MAX('table'[Date])

      RETURN

      CALCULATE( SUM(table[Net Change in Cost $]),

      DATESBETWEEN('table'[Date],startdate,enddate) ) 

       

      2nd Measure:

       

      Net Change in Cost $ Last 4 Wks =

      var startday = MAX('table'[Date])-28

      var endday = MAX('table'[Date])

      return CALCULATE( SUM('table'[Net Change in Cost $]),

      DATESBETWEEN('table'[Date],startday,endday) ) 

       

      The Date is coming from Date Slicer created in report.

       

      Let me know if I am missing out something here.

       

       

       

       

      • tamerj1's avatar
        tamerj1
        Community Champion

        Hi apatwal

        You cannot use time intelligence functions without a standard Date table. Have you tried my approach?

    • apatwal's avatar
      apatwal
      Helper III

      Hi tamerj1 

      I do not have any date table.

      There is only one table which includes all values including dates.