Forum Discussion

scolvin's avatar
scolvin
Icon for Helper I rankHelper I
7 months ago
Solved

measure with period join

Hi,

 

I have 2 tables :


- PRODUCT_VERSION :

Product_idProduct_versionStart_date End_date
A101/01/2024 31/08/2024
A201/09/2024 30/04/2025
A301/05/2025  


- PRODUCT_SALES :

Product_idSalesSales_date
A618/01/2024
A2421/09/2024
A801/03/2025


I want to calculate the sales for each product version in the PRODUCT_VERSION table.
I join the 2 tables on product_id and I create a measure : CALCULATE (sales,DATESBETWEEN(Sales_date,Start_date,End_date))

But it doesn't work, do you have an idea ?

  • Hello scolvin

    You’re running into a classic period join problem in DAX. The issue is that DATESBETWEEN expects a date column from a Date table, not row‑level start/end dates from another table. That’s why your original measure doesn’t work.

    Microsoft Documentation:

    https://learn.microsoft.com/en-us/dax/calculate-function-dax?utm_

    https://learn.microsoft.com/en-us/dax/filter-function-dax?utm_

    https://learn.microsoft.com/en-us/dax/datesbetween-function-dax?utm_
    Correct DAX Pattern

    Since your PRODUCT_VERSION table has row‑level start and end dates, use FILTER:

    Sales by Version =
    CALCULATE (
        SUM ( PRODUCT_SALES[Sales] ),
        FILTER (
            PRODUCT_SALES,
            PRODUCT_SALES[Sales_date] >= PRODUCT_VERSION[Start_date]
                && PRODUCT_SALES[Sales_date] <= PRODUCT_VERSION[End_date]
        )
    )


    This ensures that each product version row filters sales correctly.

    Alternative with a Date Table

    If you set up a proper Date table in Fabric/Power BI and relate it to PRODUCT_SALES[Sales_date], you can use:

    Sales by Version =
    CALCULATE (
        SUM ( PRODUCT_SALES[Sales] ),
        DATESBETWEEN (
            'Date'[Date],
            PRODUCT_VERSION[Start_date],
            PRODUCT_VERSION[End_date]
        )
    )

     

    This works only if:

    1. A Date table exists and is related to sales.

    2. The measure is evaluated in the context of a single product version row.

      Step‑by‑Step Example with Your Data

      PRODUCT_VERSION

      Product_id Product_version Start_date End_date
      A101/01/202431/08/2024
      A201/09/202430/04/2025
      A301/05/2025(open)
       

      PRODUCT_SALES

      Product_id Sales Sales_date
      A618/01/2024
      A2421/09/2024
      A801/05/2025

       

    Matching Sales to Versions

    1. Version 1 (01/01/2024 → 31/08/2024)

      • Sales on 18/01/2024 → falls in this range.

      • Total = 6.

    2. Version 2 (01/09/2024 → 30/04/2025)

      • Sales on 21/09/2024 → falls in this range.

      • Total = 24.

    3. Version 3 (01/05/2025 → open)

      • Sales on 01/05/2025 → falls in this range.

      • Total = 8.

        Final Results

        Product_version Sales_total
        16
        224
        38

         

    Takeaway

    • Use FILTER when comparing row‑level start/end dates.

    • Use DATESBETWEEN only with a proper Date table.

    • In Fabric, you can either solve it at the semantic model level (DAX) or at the data prep level (SQL range join).

      This approach ensures each product version correctly aggregates the sales that fall within its defined period.

     

     

5 Replies

  • The result must be 

    Product_idProduct_versionStart_date End_date Sales
    A101/01/2024 31/08/2024 6
    A201/09/2024 30/04/2025 32
    A301/05/2025    



    Thank you

    • FreemanZ's avatar
      FreemanZ
      Icon for Super User rankSuper User

      hi scolvin ,

       

      Not sure if i fully get you, you can try to add a calculated column in version table like:

      Sales = 
      SUMX(
          FILTER(
              sales,
              sales[Product_id]=[Product_id]
                  && sales[Sales_date]>=[Start_date]
                  && sales[Sales_date]<=[End_date]
          ),
          sales[Sales]
      )

       

      it works like:

  • Please try the formula below:

    Sales by Version =
    VAR _Start = SELECTEDVALUE ( PRODUCT_VERSION[Start_date] )
    VAR _End   = SELECTEDVALUE ( PRODUCT_VERSION[End_date] )
    RETURN
    CALCULATE (
        SUM ( PRODUCT_SALES[Sales] ),
        FILTER (
            PRODUCT_SALES,
            PRODUCT_SALES[Sales_date] >= _Start
                && PRODUCT_SALES[Sales_date] <= _End
        )
    )
  • Hello scolvin

    You’re running into a classic period join problem in DAX. The issue is that DATESBETWEEN expects a date column from a Date table, not row‑level start/end dates from another table. That’s why your original measure doesn’t work.

    Microsoft Documentation:

    https://learn.microsoft.com/en-us/dax/calculate-function-dax?utm_

    https://learn.microsoft.com/en-us/dax/filter-function-dax?utm_

    https://learn.microsoft.com/en-us/dax/datesbetween-function-dax?utm_
    Correct DAX Pattern

    Since your PRODUCT_VERSION table has row‑level start and end dates, use FILTER:

    Sales by Version =
    CALCULATE (
        SUM ( PRODUCT_SALES[Sales] ),
        FILTER (
            PRODUCT_SALES,
            PRODUCT_SALES[Sales_date] >= PRODUCT_VERSION[Start_date]
                && PRODUCT_SALES[Sales_date] <= PRODUCT_VERSION[End_date]
        )
    )


    This ensures that each product version row filters sales correctly.

    Alternative with a Date Table

    If you set up a proper Date table in Fabric/Power BI and relate it to PRODUCT_SALES[Sales_date], you can use:

    Sales by Version =
    CALCULATE (
        SUM ( PRODUCT_SALES[Sales] ),
        DATESBETWEEN (
            'Date'[Date],
            PRODUCT_VERSION[Start_date],
            PRODUCT_VERSION[End_date]
        )
    )

     

    This works only if:

    1. A Date table exists and is related to sales.

    2. The measure is evaluated in the context of a single product version row.

      Step‑by‑Step Example with Your Data

      PRODUCT_VERSION

      Product_id Product_version Start_date End_date
      A101/01/202431/08/2024
      A201/09/202430/04/2025
      A301/05/2025(open)
       

      PRODUCT_SALES

      Product_id Sales Sales_date
      A618/01/2024
      A2421/09/2024
      A801/05/2025

       

    Matching Sales to Versions

    1. Version 1 (01/01/2024 → 31/08/2024)

      • Sales on 18/01/2024 → falls in this range.

      • Total = 6.

    2. Version 2 (01/09/2024 → 30/04/2025)

      • Sales on 21/09/2024 → falls in this range.

      • Total = 24.

    3. Version 3 (01/05/2025 → open)

      • Sales on 01/05/2025 → falls in this range.

      • Total = 8.

        Final Results

        Product_version Sales_total
        16
        224
        38

         

    Takeaway

    • Use FILTER when comparing row‑level start/end dates.

    • Use DATESBETWEEN only with a proper Date table.

    • In Fabric, you can either solve it at the semantic model level (DAX) or at the data prep level (SQL range join).

      This approach ensures each product version correctly aggregates the sales that fall within its defined period.

     

     

  • Thank you guys 👍
    It's ok with SELECTEDVALUE(Start_date) and SELECTEDVALUE(End_date)