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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
lokosrio
Helper II
Helper II

Calculation for last date where flag is set to one

hi,

 

I need a help with a calculation.

The formula looks like this:

 

DIVIDE(
    SUMX(VALUES(table[region]), LASTNONBLANKVALUE(table[date],
        SUM(table[flag]))
    ),
    SUMX(VALUES(table[region]), LASTNONBLANKVALUE(table[date],
        COUNTROWS(table))
    )
)

 

 

 

but I do no want to have last date, instead of this I need date where at least one row have a flag = 1, for example:

regiondateflagsales
us08.03.2021010
us08.03.2021120
us09.10.2021030
us09.10.2021040

Insted of calculation for 09.10.2021 I want to calculate data for 08.03.2021, because there at least one row has flag = 1.

Could you please help me with this?

1 ACCEPTED SOLUTION

Hi @lokosrio ,

You can create a measure as below to get the percentage of per region, please find the details in the attachment.

pct_per_region =
VAR _latestdate =
    CALCULATE (
        MAX ( 'Table'[date] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[region] = SELECTEDVALUE ( 'Table'[region] )
                && 'Table'[flag] = 1
        )
    )
VAR _countflag1 =
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[product] ),
        FILTER (
            'Table',
            'Table'[region] = SELECTEDVALUE ( 'Table'[region] )
                && 'Table'[flag] = 1
                && 'Table'[date] = _latestdate
        )
    )
VAR _countldate =
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[product] ),
        FILTER (
            'Table',
            'Table'[region] = SELECTEDVALUE ( 'Table'[region] )
                && 'Table'[date] = _latestdate
        )
    )
RETURN
    DIVIDE ( _countflag1, _countldate, 0 )

yingyinr_0-1631522911533.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

8 REPLIES 8
richbenmintz
Solution Sage
Solution Sage

Hi @lokosrio,

 

Are you able to provide the expected outcome, I am not sure I quite understand the requirement for the formula



I hope this helps,
Richard

Did I answer your question? Mark my post as a solution! Kudos Appreciated!

Proud to be a Super User!


hi @richbenmintz 

I want to get the sum divided by count of rows, but it should be done on the region level.

In above example I want to sum the flags (in this case 1), dividing by count of rows (in this case 2) for the date, where at least one flag = 1 the on a region level, so the result will be 0,5.

Other example:

regiondateflag
de08.03.20210
de08.03.20210
de09.10.20211
de09.10.20210
de09.10.20211
us08.03.20210
us08.03.20211
us09.10.20210
us09.10.20210

 

in this case eu = 2/3 = 0,6 and us = 1/2 = 0,5
Sorry that I forgot to mention, but on the visualization (simple table) there will be more dimensions and filters. If something is still not clear then please let me know.

Hi @lokosrio ,

 

Given the data provided i did the following:

1. Created a Calc Column to define where the dates had flag

date_has_flag = 
var _region = 'sample'[region]
var _date = 'sample'[date]
return
if(CALCULATE(SUM('sample'[flag]), FILTER('sample', 'sample'[region] = _region && 'sample'[date] = _date))>0, 1, 0) 

2. Created a measure that used the calc column value to identify where the calc needs to take place

pct per region = 
DIVIDE(
    CALCULATE(COUNTROWS('sample'), 'sample'[date_has_flag] = 1, 'sample'[flag] = 1), CALCULATE(COUNTROWS('sample'), 'sample'[date_has_flag] =1))

gets the following result

richbenmintz_0-1631310163962.png

Hope this works for you



I hope this helps,
Richard

Did I answer your question? Mark my post as a solution! Kudos Appreciated!

Proud to be a Super User!


hi @richbenmintz,

 

it looks good, I added a small modification to get max date where the flag = 1 grouped by region, but it needs to be changed dynamically - I have a month filter on visualization, so the date_has_flag needs to consider latest date for selected months.

When I choose january 2021:

regiondateflagdate_has_flag 
us07.01.202111
us07.01.202111
us08.03.202100
us08.03.202110
us09.10.202100
us09.10.202100

 

when I choose for example january - march 2021, january-october, etc. or whole 2021:

regiondateflagdate_has_flag 
us07.01.202110
us07.01.202110
us08.03.202101
us08.03.202111
us09.10.202100
us09.10.202100

Hi @lokosrio ,

 

I am not really following, the date_has_flag column depends on the flag and region columns and does not care about the date, a date either has a flag or it does not, how you filter on the period is not really effected. 

 

Thanks,



I hope this helps,
Richard

Did I answer your question? Mark my post as a solution! Kudos Appreciated!

Proud to be a Super User!


hi @richbenmintz 

 

I will try to simplify it. First of all I modified your calculated column:

 

 

 

 

 

if(CALCULATE(SUM('sample'[flag]), FILTER('sample', 'sample'[region] = _region && CALCULATE(MAX('sample'[date]),
ALLEXCEPT('sample', 'sample'[region]), sample[flag] = 1) = _date))>0, 1, 0) 

 

 

 

 

 

it is fine, but the date_has_flag is fixed on table level.

I want to change it - date_has_flag needs to change dynamically and be depended on period filter.

 

Sample data:

 

productregiondateflag
prod_1de08.03.20210
prod_2de08.03.20210
prod_3de09.10.20211
prod_4de09.10.20210
prod_5de09.10.20211
prod_6us07.01.20211
prod_7us07.01.20211
prod_8us08.03.20210
prod_9us08.03.20211
prod_10us09.10.20210
prod_11us09.10.20210

Example1: When I choose january 2021 in the period filter

the result on the visualization is:

regionpct_per_region
de0
us1

it is because there is no data for de in january and for us the latest date, where at least one row has flag = 1 is on 07.01.2021, so sum of flag is 2 devided by count of rows 2 = 1

 

Example2: when I choose for example january - november 2021 in the period filter:

regionpct_per_region
de0,6
us0,5

latest date, where at least one row has flag= 1 for de is 09.10.2021, so the result is 2/3 = 0,67

latest date, where at least one row has flag = 1 for us is 08.03.2021, so the result is 1/2 = 0,5

 

Please let me know if you get the point.

Hi @lokosrio ,

You can create a measure as below to get the percentage of per region, please find the details in the attachment.

pct_per_region =
VAR _latestdate =
    CALCULATE (
        MAX ( 'Table'[date] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[region] = SELECTEDVALUE ( 'Table'[region] )
                && 'Table'[flag] = 1
        )
    )
VAR _countflag1 =
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[product] ),
        FILTER (
            'Table',
            'Table'[region] = SELECTEDVALUE ( 'Table'[region] )
                && 'Table'[flag] = 1
                && 'Table'[date] = _latestdate
        )
    )
VAR _countldate =
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[product] ),
        FILTER (
            'Table',
            'Table'[region] = SELECTEDVALUE ( 'Table'[region] )
                && 'Table'[date] = _latestdate
        )
    )
RETURN
    DIVIDE ( _countflag1, _countldate, 0 )

yingyinr_0-1631522911533.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @lokosrio,

 

try this, with the original date_has_flag calculation

pct per region = 
var _max_date = CALCULATE(MAX('sample'[date]), 'sample'[flag] =1)
return
DIVIDE(
    CALCULATE(COUNTROWS('sample'), 'sample'[date_has_flag] = 1, 'sample'[flag] = 1, 'sample'[date] = _max_date), 
    CALCULATE(COUNTROWS('sample'), 'sample'[date_has_flag] =1, 'sample'[date] =_max_date))

 



I hope this helps,
Richard

Did I answer your question? Mark my post as a solution! Kudos Appreciated!

Proud to be a Super User!


Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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