Forum Discussion

fjjohann's avatar
fjjohann
Frequent Visitor
9 years ago
Solved

DatesBetween

HI!   I've 2 Tables   TABLE 1: Column 1: Name Column 2: Percent Column 3: Start Date Column 4: End Date   TABLE 2 Column 1: Name Column 2: Value   -----   I need a calculation for the...
  • OwenAuger's avatar
    OwenAuger
    9 years ago

    Hi fjjohann

     

    Here is a suggestion for how to set up the data model to handle this with DAX.

    PBIX sample here (used your Bill/Microsoft example and added Steve)

     

    1. Set up the data model like this (bi-directional relationship between Ownership/Company not strictly required for this measure but may be useful for other purposes):
    2. Create this measure to calculate Expense Weighted by Ownership:
      Expense Weighted by Ownership = 
      SUMX (
          GENERATE (
              ADDCOLUMNS (
                  SUMMARIZE (
                      Ownership,
                      Company[Company],
                      Ownership[Start Date],
                      Ownership[End Date]
                  ),
                  "ParticipationTotal", CALCULATE ( SUM ( Ownership[Participation] ) )
              ),
              INTERSECT (
                  DATESBETWEEN ( 'Date'[Date], Ownership[Start Date], Ownership[End Date] ),
                  VALUES ( Expense[Date] ) // This could also be VALUES ( 'Date'[Date] )
              )
          ),
          [ParticipationTotal] * CALCULATE ( SUM ( Expense[Value] ) )
      )
      I am drawing on 'events in progress' patterns from Gerhard Brueckl and SQLBI.
      The GENERATE part of the measure creates a table of Company/Date/Participation combinations, then SUMX sums Participation * Expense for each of those combinations.
    3. Then any visual filtered by Ownership[Owner], Company[Company] and the Date table will show the Weighted Expense:

       

       

    Alternatives:

    • You could modify your Ownership table so that the Start/End dates are instead converted to a single Date column covering the date range of ownership, something like here: https://www.sqlbi.com/articles/analyzing-events-with-a-duration-in-dax/
    • Or maybe you could pre-process the entire allocation of Expenses to Owner/Company combinations in the data load through the Query Editor.

    Cheers,

    Owen