Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Use variable as a filter in CALCULATE - error

I need to calculate the amount of active students going back in time. 

I have one date column, with an event type for each student.

 

In order to see if they are active, it requires a series of datediff calculations. But first I had to calculate the dates I needed out from the single date column. I tried this using variables:

 

B TEST 0704 =
var first_exam = CALCULATE(MIN('Demand'[CreatedDate]), ALL('Demand'),'Demand'[CitizenId] = MAX('Demand'[CitizenId]),
'Demand'[Event Type] = 4 || 'Demand'[Event Type] = 5 || 'Demand'[Event Type] = 9 || 'Demand'[Event Type] = 12 || 'Demand'[Event Type] = 21 || 'Demand'[Event Type] = 22 || 'Demand'[Event Type] = 23 || 'Demand'[Event Type] = 24 || 'Demand'[Event Type] = 25 || 'Demand'[Event Type] = 26 || 'Demand'[Event Type] = 27 || 'Demand'[Event Type] = 28)

 

var latest_exam = CALCULATE(MAX('Demand'[CreatedDate]), ALL('Demand'),'Demand'[CitizenId] = MAX('Demand'[CitizenId]),
'Demand'[Event Type] = 4 || 'Demand'[Event Type] = 5 || 'Demand'[Event Type] = 9 || 'Demand'[Event Type] = 12 || 'Demand'[Event Type] = 21 || 'Demand'[Event Type] = 22 || 'Demand'[Event Type] = 23 || 'Demand'[Event Type] = 24 || 'Demand'[Event Type] = 25 || 'Demand'[Event Type] = 26 || 'Demand'[Event Type] = 27 || 'Demand'[Event Type] = 28)

 

var Accepted_date = CALCULATE(MIN('Demand'[CreatedDate]), ALL('Demand'),'Demand'[CitizenId] = MAX('Demand'[CitizenId]), 'Demand'[Event Type] = 1)

 

var Doctor_date = CALCULATE(MIN('Demand'[CreatedDate]), ALL('Demand'),'Demand'[CitizenId] = MAX('Demand'[CitizenId]), 'Demand'[Event Type] = 16)

 

var current_date = max('Date'[Date])

 

var exam_passed = CALCULATE(MIN('Demand'[CreatedDate]), ALL('Demand'),'Demand'[CitizenId] = MAX('Demand'[CitizenId]),'Demand'[Event Type] = 10)

 

var theory_passed = CALCULATE(MIN('Demand'[CreatedDate]), ALL('Demand'),'Demand'[CitizenId] = MAX('Demand'[CitizenId]),'Demand'[Event Type] = 4)

 

return

calculate(
distinctcount( 'Demand'[citizenid] ),
Kategori[Kategori] = "B",
'Demand'[Application_paid?] = TRUE(),
ISBLANK(exam_passed),
not(isblank(Doctor_date)),
not(ISBLANK(Accepted_date)),
datediff(first_exam, latest_exam,DAY) > 0 && datediff(first_exam, latest_exam,DAY) < 92 || datediff(Accepted_date, current_date,DAY) < 183,
datediff(Accepted_date, first_exam, DAY) > 0 && datediff(Accepted_date, first_exam, DAY) < 183 || datediff(Accepted_date, current_date,DAY) < 183,
datediff(Doctor_date, current_date, DAY) < 365,
datediff(latest_exam, current_date,DAY) > 0 && datediff(latest_exam, current_date,DAY) < 92 || datediff(Accepted_date, current_date,DAY) < 183

 

However I received an error message along the lines of: "True/False expression does not give one specific column. Every True/False expression, that is used as a table-filter expression must refer to exactly one column".

 

Is there any way to fix this measure or get around the error?

1 ACCEPTED SOLUTION
Whitewater100
Solution Sage
Solution Sage

Hello:

Thisisn't directly answering yourquestion, but hopefully a much easier way to solve for any analysis you might want, with minimal DAX. Designing the data model wit Star Schema.  I'll paste the file link here and hopefully this approach could work for you. I put in just a few basic measures for examples.

 

https://drive.google.com/file/d/17hxnOKiykG21T-NvbT0BbVbTRFY2t2cM/view?usp=sharing 

 

Whitewater100_0-1649684366847.png

 

View solution in original post

4 REPLIES 4
Whitewater100
Solution Sage
Solution Sage

Hello:

Thisisn't directly answering yourquestion, but hopefully a much easier way to solve for any analysis you might want, with minimal DAX. Designing the data model wit Star Schema.  I'll paste the file link here and hopefully this approach could work for you. I put in just a few basic measures for examples.

 

https://drive.google.com/file/d/17hxnOKiykG21T-NvbT0BbVbTRFY2t2cM/view?usp=sharing 

 

Whitewater100_0-1649684366847.png

 

Anonymous
Not applicable

Hi there,

 

This is useful, thank you for that. This is definitely something I should implement.

Samarth_18
Community Champion
Community Champion

HI @Anonymous ,

 

Its bit difficult to answer without sample data with expected output but you can try this:-

B TEST 0704 =
VAR first_exam =
    CALCULATE (
        MIN ( 'Demand'[CreatedDate] ),
        ALL ( 'Demand' ),
        'Demand'[CitizenId] = MAX ( 'Demand'[CitizenId] ),
        'Demand'[Event Type] = 4
            || 'Demand'[Event Type] = 5
            || 'Demand'[Event Type] = 9
            || 'Demand'[Event Type] = 12
            || 'Demand'[Event Type] = 21
            || 'Demand'[Event Type] = 22
            || 'Demand'[Event Type] = 23
            || 'Demand'[Event Type] = 24
            || 'Demand'[Event Type] = 25
            || 'Demand'[Event Type] = 26
            || 'Demand'[Event Type] = 27
            || 'Demand'[Event Type] = 28
    )
VAR latest_exam =
    CALCULATE (
        MAX ( 'Demand'[CreatedDate] ),
        ALL ( 'Demand' ),
        'Demand'[CitizenId] = MAX ( 'Demand'[CitizenId] ),
        'Demand'[Event Type] = 4
            || 'Demand'[Event Type] = 5
            || 'Demand'[Event Type] = 9
            || 'Demand'[Event Type] = 12
            || 'Demand'[Event Type] = 21
            || 'Demand'[Event Type] = 22
            || 'Demand'[Event Type] = 23
            || 'Demand'[Event Type] = 24
            || 'Demand'[Event Type] = 25
            || 'Demand'[Event Type] = 26
            || 'Demand'[Event Type] = 27
            || 'Demand'[Event Type] = 28
    )
VAR Accepted_date =
    CALCULATE (
        MIN ( 'Demand'[CreatedDate] ),
        ALL ( 'Demand' ),
        'Demand'[CitizenId] = MAX ( 'Demand'[CitizenId] ),
        'Demand'[Event Type] = 1
    )
VAR Doctor_date =
    CALCULATE (
        MIN ( 'Demand'[CreatedDate] ),
        ALL ( 'Demand' ),
        'Demand'[CitizenId] = MAX ( 'Demand'[CitizenId] ),
        'Demand'[Event Type] = 16
    )
VAR current_date =
    MAX ( 'Date'[Date] )
VAR exam_passed =
    CALCULATE (
        MIN ( 'Demand'[CreatedDate] ),
        ALL ( 'Demand' ),
        'Demand'[CitizenId] = MAX ( 'Demand'[CitizenId] ),
        'Demand'[Event Type] = 10
    )
VAR theory_passed =
    CALCULATE (
        MIN ( 'Demand'[CreatedDate] ),
        ALL ( 'Demand' ),
        'Demand'[CitizenId] = MAX ( 'Demand'[CitizenId] ),
        'Demand'[Event Type] = 4
    )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( 'Demand'[citizenid] ),
        Kategori[Kategori] = "B",
        FILTER (
            'Demand',
            'Demand'[Application_paid?] = TRUE ()
                && ISBLANK ( exam_passed )
                && NOT ( ISBLANK ( Doctor_date ) )
                    && NOT ( ISBLANK ( Accepted_date ) )
                        && (
                            DATEDIFF ( first_exam, latest_exam, DAY ) > 0
                                && DATEDIFF ( first_exam, latest_exam, DAY ) < 92
                        )
                        || DATEDIFF ( Accepted_date, current_date, DAY ) < 183
                            && (
                                DATEDIFF ( Accepted_date, first_exam, DAY ) > 0
                                    && DATEDIFF ( Accepted_date, first_exam, DAY )
                            ) < 183
                        || DATEDIFF ( Accepted_date, current_date, DAY ) < 183
                            && DATEDIFF ( Doctor_date, current_date, DAY ) < 365
                            && (
                                DATEDIFF ( latest_exam, current_date, DAY ) > 0
                                    && DATEDIFF ( latest_exam, current_date, DAY ) < 92
                            )
                        || DATEDIFF ( Accepted_date, current_date, DAY ) < 183
        )
    )

 

Thanks,

Samarth

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Anonymous
Not applicable

@Samarth_18 Thanks for your response 

 

This gets around the error message, but the filters themseleves are not actually working.

 

I unfortunately cant share the data, but I can give an example of what it looks like: 

 

IDNameEvent TypeEvent Type no.Event Date
1000001John SmithDoctor Certification1601-12-2021
1000001John SmithExam Passed1001-03-2022
1000001John SmithExam Failed731-01-2022
1000001John SmithDid not show up to exam610-01-2022
1000001John SmithApplication Accepted130-11-2021
1000005Sarah AdamsExam Failed718-01-2022
1000005Sarah AdamsDid not show up to exam630-01-2022
1000005Sarah AdamsApplication Accepted101-12-2021
1000005Sarah AdamsDoctor Certification1601-12-2021
1000005Sarah AdamsExam Failed720-02-2022
1000010David JonesApplication Accepted101-06-2021
1000010David JonesDoctor Certification1601-06-2021
1000020Kirsty RobertsApplication Accepted101-03-2022
1000020Kirsty RobertsDoctor Certification1601-03-2022
1000022James BondApplication Accepted101-02-2022
1000022James BondDoctor Certification1602-02-2022
1000022James BondExam Failed715-03-2022

 

 

In this scenario David Jones and John Smith should not be considered as active, John Smith has passed the exam, and David Jones has not done anything in the last 183 days. So there should be 3 active students.

 

Please let me know if there is anything else you may need

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.