Forum Discussion

rohtashgoyal's avatar
rohtashgoyal
New Member
4 years ago
Solved

Need to create a visual equivalent to minus in SQL

I want to achieve below using power bi visual, but don't want to write any raw sql, is that possible?

for example for attendance I want to show list of students who attended class on Jan 3rd, but did't attend on Jan ist.

roll_table has 3 fields only id, name, date, same table records daily rolls for all dates.

so I write

Select  id , name from attendance where date='2022-01-03'

Minus

Select  id , name from attendance where date='2022-01-01'

so it will give me list of students I want.

How to achieve this in Power bi.

  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi rohtashgoyal ,

    I created a sample pbix file(see attachment) for you, please check whether that is what you want. 

    1. Create two date dimension tables and apply the date field on the date slicers

    2. Create a measure as below 

    Flag = 
    VAR _date1 =
        SELECTEDVALUE('Attend on'[Date])
    VAR _date2 =
        SELECTEDVALUE('Not attend on'[Date])
    VAR _tab1 =
        CALCULATETABLE (
            VALUES ( 'Table'[name] ),
            FILTER ( 'Table', 'Table'[date] = _date1 )
        )
    VAR _tab2 =
        CALCULATETABLE (
            VALUES ( 'Table'[name] ),
            FILTER ( 'Table', 'Table'[date] = _date2 )
        )
    RETURN
        IF ( SELECTEDVALUE ( 'Table'[name] ) IN EXCEPT ( _tab1, _tab2 ), 1, 0 )

    3. Create a visual and apply a visual-level filter with the condition(Flag is 1)

    If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

    How to upload PBI in Community

    Best Regards

2 Replies

  • if will be great if I can have two drop down filtes to select dates, so that users have flexibility.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi rohtashgoyal ,

    I created a sample pbix file(see attachment) for you, please check whether that is what you want. 

    1. Create two date dimension tables and apply the date field on the date slicers

    2. Create a measure as below 

    Flag = 
    VAR _date1 =
        SELECTEDVALUE('Attend on'[Date])
    VAR _date2 =
        SELECTEDVALUE('Not attend on'[Date])
    VAR _tab1 =
        CALCULATETABLE (
            VALUES ( 'Table'[name] ),
            FILTER ( 'Table', 'Table'[date] = _date1 )
        )
    VAR _tab2 =
        CALCULATETABLE (
            VALUES ( 'Table'[name] ),
            FILTER ( 'Table', 'Table'[date] = _date2 )
        )
    RETURN
        IF ( SELECTEDVALUE ( 'Table'[name] ) IN EXCEPT ( _tab1, _tab2 ), 1, 0 )

    3. Create a visual and apply a visual-level filter with the condition(Flag is 1)

    If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

    How to upload PBI in Community

    Best Regards