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
helanan
Frequent Visitor

Help with trying to get a sum of a columns count values based off of a selected date

I am currently completely stumped on this problem I am having and am not sure if this is possible within power bi but I am trying to filter my visualization by one slicers single date value, and by entity id, test name and test type then calculate a current count by summing the count column based off of those filters.  Then I would like to perform another calculation where I sum the count column again but this count would be based on a different selected single-day date value. This second count value would be capturing the selected previous period's count. 

 

helanan_0-1691187937614.png

 

1 ACCEPTED SOLUTION
Martin_D
Super User
Super User

Hi @helanan ,

Yes, that's possible to do with Power BI. You need the following:

 

  1. The first date slicer must use a date table that is related to your test ledger table in a unidirectional one-to-many relationship, see screenshot of data model, because if you use the column from the ledger table you cannot turn off the relationship between the dates and the ledger entries, see below.
  2. The second date slicer must come from a disconnected date table, i.e. a second date table that has no relationship to any other table in the data model. The second date table is just a renamed copy of the first date table.
    Data model with disconncted date tableData model with disconncted date table
  3. You can probaly keep your first measure as it is, showing a value based on the first date slicer.
    DAX code of a measure:
    SelectedPeriodCountTests = COUNTROWS ( 'Test Ledger' )
  4. In order to apply the second date slicer, you need a measure that turns off the relationship between the first date slicer and the ledger table and activates a relationship between the second date table and the ledger table. You can use variables if you want to include a measure filtered by the first date slicer and a value calculated based on the second date slicer in your calculation in the same measure, like this.
    DAX code of a measure:
    DifferentSelectedDateMeasureReferencingSelectedPeriodCount =
    // This first measure is calculated based on first date slicer and not impacted by second date slicer
    VAR
    _SelectedPeriodCount = [SelectedPeriodCountTests]
    // This variable is calulated based on second date slicer and not impacted by first date slicer
    VAR
    _MeasureBasedOnSecondDateSlicer =
        CALCULATE (
            // Some calculation that you want to do based on thesecond date slicer
            COUNTROWS ( 'Test Ledger' ),
            // Turn off relationship between date table and ledger table using parameter value "None"
            CROSSFILTER ( 'Test Ledger'[Date Key], 'Date Table'[Date Key], None ),
            // Activate virtual filter relationship between second date table and ledger table using TREATAS function
            TREATAS ( VALUES ( 'Disconnected Date Table'[Date Key]), 'Test Ledger'[Date Key] )
        )
    // Do some calculation that uses values based on both date slicers, e.g.
    VAR _Deviation = DIVIDE ( _MeasureBasedOnSecondDateSlicer - _SelectedPEriodCount, _SelectedPeriodCount )
    RETURN
    _Deviation

Some might recommend you to create an inactive relationship in the data model instead of disconnected table and use filter functions USERELATIONSHIP or CROSSFILTER. These solutions would be equally valid as long as they are complete and include two date tables. The reason I did not choose them is because they are most beneficial if you have two table, e.g. date and ledger, and two relationships between them, because then using USERELATIONSHIP would implicitly turn off the active relationship. Since we need two date tables because we want to feed two independant (not hierarchical) date slicers we need to turn off the relationship between the first date table and the ledger table explicitly in the measure code anyway.

BR

Martin

github.pnglinkedin.png

View solution in original post

3 REPLIES 3
helanan
Frequent Visitor

Thank you so much @Martin_D this was a HUGE help and worked the only issue I am running into now is that I cant get it to filter by the minimum selected start date.  I am trying to make the second date slicer filter by a single day value by using the after filter in the date picker but cannot figure out how to then get this to be filtered down to only 1 selected days worth of data.  Im not sure if you know the answer to that as well, thank you again! 

The only options for a true single select date slicer are dropdown, verticle list, or tile slicer format. As a workaround with data picker you could choose the on or before setting and select only the maximum selected date in the measure like:

 

DifferentSelectedDateMeasureReferencingSelectedPeriodCount =

VAR _SelectedSecondDate = MAX ( 'Disconnected Date Table'[Date Key] )

... // continue with code as before, only change TREATAS line to

TREATAS ( { _SelectedSecondDate }, 'Test Ledger'[Date Key] )
...

Martin_D
Super User
Super User

Hi @helanan ,

Yes, that's possible to do with Power BI. You need the following:

 

  1. The first date slicer must use a date table that is related to your test ledger table in a unidirectional one-to-many relationship, see screenshot of data model, because if you use the column from the ledger table you cannot turn off the relationship between the dates and the ledger entries, see below.
  2. The second date slicer must come from a disconnected date table, i.e. a second date table that has no relationship to any other table in the data model. The second date table is just a renamed copy of the first date table.
    Data model with disconncted date tableData model with disconncted date table
  3. You can probaly keep your first measure as it is, showing a value based on the first date slicer.
    DAX code of a measure:
    SelectedPeriodCountTests = COUNTROWS ( 'Test Ledger' )
  4. In order to apply the second date slicer, you need a measure that turns off the relationship between the first date slicer and the ledger table and activates a relationship between the second date table and the ledger table. You can use variables if you want to include a measure filtered by the first date slicer and a value calculated based on the second date slicer in your calculation in the same measure, like this.
    DAX code of a measure:
    DifferentSelectedDateMeasureReferencingSelectedPeriodCount =
    // This first measure is calculated based on first date slicer and not impacted by second date slicer
    VAR
    _SelectedPeriodCount = [SelectedPeriodCountTests]
    // This variable is calulated based on second date slicer and not impacted by first date slicer
    VAR
    _MeasureBasedOnSecondDateSlicer =
        CALCULATE (
            // Some calculation that you want to do based on thesecond date slicer
            COUNTROWS ( 'Test Ledger' ),
            // Turn off relationship between date table and ledger table using parameter value "None"
            CROSSFILTER ( 'Test Ledger'[Date Key], 'Date Table'[Date Key], None ),
            // Activate virtual filter relationship between second date table and ledger table using TREATAS function
            TREATAS ( VALUES ( 'Disconnected Date Table'[Date Key]), 'Test Ledger'[Date Key] )
        )
    // Do some calculation that uses values based on both date slicers, e.g.
    VAR _Deviation = DIVIDE ( _MeasureBasedOnSecondDateSlicer - _SelectedPEriodCount, _SelectedPeriodCount )
    RETURN
    _Deviation

Some might recommend you to create an inactive relationship in the data model instead of disconnected table and use filter functions USERELATIONSHIP or CROSSFILTER. These solutions would be equally valid as long as they are complete and include two date tables. The reason I did not choose them is because they are most beneficial if you have two table, e.g. date and ledger, and two relationships between them, because then using USERELATIONSHIP would implicitly turn off the active relationship. Since we need two date tables because we want to feed two independant (not hierarchical) date slicers we need to turn off the relationship between the first date table and the ledger table explicitly in the measure code anyway.

BR

Martin

github.pnglinkedin.png

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.