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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
TungNguyen_19
Helper III
Helper III

How to use isinscope correctly to calculate the average of each category?

Hi everyone,

I have this sample data below to calculate average of frequency appears of bills in each category:

 

B/L NbrsREASONREMARKSREMARKS.HEAD
MEDUFQ40582Shipper's RequestAMEND NACAMEND
MEDUV965302COLAMEND PAYMENT LOCALTION EE ANTWERPAMEND
MEDUFQ43441Shipper's RequestAMEND GWAMEND
MEDUFQ40211Shipper's RequestAMEND DESCRIPTIONAMEND
MEDUFQ37619SwitchAMEND SHPR, CNEE, NOTIFYAMEND
MEDUFQ42926Shipper's RequestCHANGE SWBCHANGE
MEDUFQ37837Shipper's RequestUPDATE SWBUPDATE
MEDUFQ36389Tariff UpdateADD DMG CHARGE - SHIPPER REQUESTADD
MEDUFQ25061Tariff UpdateMSC AUSTRALIA REQUEST TO REMOVE SPD FEE FOR CUSTOMER- IFF ACCOUNTMSC
MEDUFQ50474Tariff UpdateUPDATE TO CORRECT RATE - MSC RUSSIA UPDATED TMS AFTER DTX CLOSINGUPDATE
MEDUFQ50648Shipper's RequestAMEND CNEE, NOTIFYAMEND
MEDUFQ50647Shipper's RequestAMEND CNEE, NOTIFYAMEND
MEDUFQ47525Shipper's RequestAMEND CTNSAMEND
MEDUFQ51631Shipper's RequestADD FREE TIME AT DEST ON BILLADD
MEDUFQ51042Shipper's RequestREVISE SEALREVISE
MEDUFQ52567Shipper's RequestUPDATE OBL AT DESTUPDATE
MEDUFQ34475Shipper's RequestAMEND CNEE+NOTIFYAMEND
MEDUFQ349435Shipper's RequestDESCRIPTIONDESCRIPTION
MEDUFQ346795Shipper's RequestSWBSWB
MEDUFQ388698Shipper's RequestUPDAT SWBUPDAT
MEDUFQ398846Tariff UpdateFILING ERRORFILING
MEDUFQ425979Tariff UpdateFILING ERROR FILING
MEDUFQ412167Tariff UpdateFILING ERRORFILING
MEDUFQ411441Tariff UpdateFILING ERRORFILING
MEDUFQ410823Tariff UpdateAMD ONCAMD
MEDUFQ386403Change of DestinationREMOVE FINAL DESTINATIONREMOVE
MEDUFQ282503Change of DestinationREMOVE FINAL DESTINATIONREMOVE
MEDUFQ424675Custom's RequestADD VENT SEALADD
MEDUFQ410823Change of DestinationSHIPBACKSHIPBACK
MEDUFQ306914Clerical ErrorAMD FRTAMD
MEDUFQ375885Custom's RequestUPDATE CARGO OF VALUEUPDATE
MEDUFQ362982Shipper's RequestUPDATE SWBUPDATE

 

 Now I want to calulate average B.L of each category by each level of category: Reason > Remarks.Head > Remarks and I use this measure:

 

Avg_Per_Reason =
IF(
ISINSCOPE('M EMC Details'[REMARKS])
, DIVIDE([No_Remark_Per_Remark.Head], [All_Remark_Per_Remark.Head], 0)
, IF(ISINSCOPE('M EMC Details'[REMARKS.HEAD])
, DIVIDE([No_Remark.Head_Per_Reason], [All_Remark_Per_Reason], 0)
, IF(ISINSCOPE('M EMC Details'[REASON])
, DIVIDE([All_Remark_Per_Reason], [All_Reason], 0)
, 1
)
)
)

 

 

 

For the other measures, here are all remain measures's fomular:
 

No_Each_Reason_In_All_Reason = COUNT('M EMC Details'[REASON])

All_Reason = CALCULATE(
COUNT('M EMC Details'[REASON]),
ALLEXCEPT('M EMC Details','M EMC Details'[B/L Nbrs])
)

 

No_Remark_Per_Remark.Head =
CALCULATE(
COUNT('M EMC Details'[REMARKS]),
ALLEXCEPT('M EMC Details','M EMC Details'[REMARKS.HEAD], 'M EMC Details'[REMARKS])
)
All_Remark_Per_Remark.Head =

Details'[REASON]
CALCULATE(
COUNT('M EMC Details'[REMARKS.HEAD]),
ALLEXCEPT('M EMC Details','M EMC Details'[REASON], 'M EMC Details'[REMARKS.HEAD])
)


No_Remark.Head_Per_Reason =
CALCULATE(
COUNT('M EMC Details'[REMARKS.HEAD]),
ALLEXCEPT('M EMC Details','M EMC Details'[REASON], 'M EMC Details'[REMARKS.HEAD])
)

All_Remark_Per_Reason =
CALCULATE(
COUNT('M EMC Details'[REASON]),
ALLEXCEPT('M EMC Details','M EMC Details'[REASON])
)

 

 The chart look like this:

 

TungNguyen_19_1-1701404636050.png

 

 

 

 

 

 

 

 

 

 

The result I expect when I expand to each [Remark.Head]: The accumulate of average of all items Remark in that [Remark.Head] will be 100%. Ex: "REMOVE CONT" in CLC > REMOVE should be 100%

 

Same with [Reason], sum all [Remark.Head] of each reason will be accumulate to 100%.  One important thing that all [Remark.Head] will be calculate average partition for each reason.

 

One another thing is when change the date slicer the measure does not reflect the changes!

 

But my result is mismatch what I expect, can anyone please help me! Many thanks for you guy

1 ACCEPTED SOLUTION

You modified my version in a way that I don't agree with. Don't use multiple ALLSELECTED. Don't run multiple identical calculations (use variables instead).

 

PLease use this version

InCategory =
-- grab the date range for the entire table so we can reapply it for the individual items
VAR d =
    ALLSELECTED ( 'Table'[DATE] ) 
-- get the total number of entries for all siblings in the current scope level
VAR a =
    SWITCH (
        TRUE (),
        ISINSCOPE ( 'Table'[REMARKS] ), CALCULATE ( COUNTROWS ( 'Table' ), d, REMOVEFILTERS ( 'Table'[REMARKS] ) ),
        ISINSCOPE ( 'Table'[REMARKS.HEAD] ), CALCULATE ( COUNTROWS ( 'Table' ), d, REMOVEFILTERS ( 'Table'[REMARKS.HEAD] ) ),
        CALCULATE ( COUNTROWS ( 'Table' ), d, REMOVEFILTERS ( 'Table'[REASON] ) )
    ) 
-- percentage of current item number of row versus parent number of rows
RETURN
    DIVIDE ( COUNTROWS ( 'Table' ), a, 0 )

 

View solution in original post

14 REPLIES 14
lbendlin
Super User
Super User

Something like this?

lbendlin_0-1701571122429.png

 

Hi @lbendlin ,
I have tested your measure fomular but some results are not exactly right like this one (Dummy > Update > ...). In details: The [Remark.Head] = UPDATE is 100% is right because Dummy contains only UPDATE. But each [Remark] inside it should be calculated in percentage which is accumulated to 100% (currently each of them is 100%). Can you please double check it?

 

I can generate the sample data for you for easier checking:

 

B/L NbrsREASONREMARKSREMARKS.HEAD
MEDUFQ43753DummyUPDATE TMS CNEE NOTIFYUPDATE
MEDUFQ31619DummyUPDATE TMS CNEE NOTIFYUPDATE
MEDUFQ24357DummyUPDATE TMS CNEE NOTIFYUPDATE
MEDUFQ14683DummyUPDATE TMS OF CNEEUPDATE
MEDUFQ11076DummyUPDATE CONSIGNEE-NOTIFY- CODE US004070 UPDATE ON 1-SEP AFTER DTXUPDATE
MEDUFQ10164DummyUPDATE TMS CODE CNEE & CH AFTER DTX CLOSINGUPDATE
MEDUFQ12778DummyUPDATE TMS OF CNEEUPDATE
MEDUFQ47257DummyUPDATE CNEEUPDATE
MEDUFQ42511DummyUPDATE TMS CNEE/NOTIFYUPDATE
MEDUFQ37027DummyUPDATE TMS CNEE, NOTIFYUPDATE
MEDUFQ37019DummyUPDATE TMS CNEE, NOTIFYUPDATE
MEDUFQ37947DummyUPDATE TMS CNEEUPDATE
MEDUFQ36774DummyUPDATE TMS CONSNEE+NOTIFY PARTYUPDATE
MEDUFQ35339DummyUPDATE TMS CODE CNEE & NOTIFYUPDATE
MEDUFQ32677DummyUPDATE TMS CNEE & NOTIFYUPDATE
MEDUFQ29403DummyUPDATE TMS NOTIFY PARTYUPDATE
MEDUFQ28027DummyUPDATE TMS NOTIFY PARTY 2UPDATE
MEDUFQ22459DummyUPDATE TMS NOTIFY PARTY 2UPDATE
MEDUFQ27647DummyUPDATE TMS CNEE NOTIFYUPDATE
MEDUFQ27647DummyUPDATE TMS CNEE NOTIFYUPDATE
MEDUFQ27642DummyUPDATE TMS CNEE NOTIFYUPDATE
MEDUFQ24512DummyUPDATE TMS CNEEUPDATE
MEDUFQ27080DummyUPDATE TMS FOR CNEE/NOTIFYUPDATE
MEDUFQ16641DummyUPDATE TMS CODE FOR CNEE/NOTIFYUPDATE
MEDUFQ17375DummyUPDATE TMS CODE CNEE & NOTIFYUPDATE
MEDUFQ17451DummyUPDATE TMS CODE FOR NOTIFY 2UPDATE
MEDUFQ23476DummyUPDATE TMS CONSINGEE/ NOTIFY

UPDATE

 

TungNguyen_19_1-1701662815959.png

Here is what I get based on your new sample data:

 

lbendlin_0-1701742482989.png

 

Hi @lbendlin 
- Sorry that I have only sent you a part of Dummy. If you see the picture below you will find all the [Remark.Head] "Dummy" which contains 8 items. 

- Let's take an example UPDATE which is 29 records. The result of measure hear should be 29/51 = 57% but the current returns from your fomular is 82,29%. Also WISE can not be 100%...
- I also want to note that other [Reason] can have the same [Remark.Head] too (you can see in the picture attached with).
* Note that: The result should be reflected with the Date filter too. I have tested the numerator and it return true, I think the problem come from denominator but I'm not sure what is the problem here.

Can you please check it again for my case? If you need any information you can ask me

 

TungNguyen_19_1-1701747082373.png

Please provide sample data that fully covers your issue.
Please show the expected outcome based on the sample data you provided.

Hi @lbendlin 

This is my full cover issue sample data

Today when I try to paste the matrix to another tab it return correct data which I believe the error is related to the filter slicer date.

1/ With slicer it returns 100% for all [REMARK] items:

TungNguyen_19_0-1701918848614.png

2/ Without slicer it returns correct average metrics:

TungNguyen_19_1-1701918923585.png

 

Can you please fix the fomular to return correct data with the slicer filter because i have to import this filter to view data from any point of time? Many thanks sir

for some reason REMOVEFILTERS is misbehaving.  Here is an alternative implementation

 

In Category = 
var rs = max('Table'[REASON])
var rh = max('Table'[REMARKS.HEAD])
var a = switch(TRUE(),
ISINSCOPE('Table'[REMARKS]),CALCULATE(COUNTROWS('Table'),ALLSELECTED('Table'),'Table'[REASON]=rs,'Table'[REMARKS.HEAD]=rh),
ISINSCOPE('Table'[REMARKS.HEAD]),CALCULATE(COUNTROWS('Table'),ALLSELECTED('Table'),'Table'[REASON]=rs),
CALCULATE(COUNTROWS('Table'),ALLSELECTED('Table')))
return divide(COUNTROWS('Table'),a,0)

Even that is insufficient.

 

 

In Category = 
var rs = max('Table'[REASON])
var rh = max('Table'[REMARKS.HEAD])
var d = ALLSELECTED('Table'[DATE])
var a = switch(TRUE(),
ISINSCOPE('Table'[REMARKS]),CALCULATE(COUNTROWS('Table'),ALLselected('Table'),d,'Table'[REASON]=rs,'Table'[REMARKS.HEAD]=rh),
ISINSCOPE('Table'[REMARKS.HEAD]),CALCULATE(COUNTROWS('Table'),ALLSELECTED('Table'),d,'Table'[REASON]=rs),
CALCULATE(COUNTROWS('Table'),ALLSELECTED('Table'),d))
return  divide(COUNTROWS('Table'),a,0)

 

Thanks @lbendlin 
- The fomular works perfectly ❤️ But can I ask that why you use all these filters in each CALCULATE scope - This fomular is really complicated for me and I have no clues how does it worked? 😢

- If you don't mind please explain how your solution worked in details and I will use this sample for the completed tutorial for this function on my blog (which refer to your answer) for anyone else face to this problem! I think this will help a lot for many people.

REMOVEFILTERS worked as designed but the complexity came from the date range slicer. Once that is taken care of the original formula works as well

 

 

 

InCategory =
-- grab the date range for the entire table so we can reapply it for the individual items
VAR d =
    ALLSELECTED ( 'Table'[DATE] ) 
-- get the total number of entries for all siblings in the current scope level
VAR a =
    SWITCH (
        TRUE (),
        ISINSCOPE ( 'Table'[REMARKS] ), CALCULATE ( COUNTROWS ( 'Table' ), d, REMOVEFILTERS ( 'Table'[REMARKS] ) ),
        ISINSCOPE ( 'Table'[REMARKS.HEAD] ), CALCULATE ( COUNTROWS ( 'Table' ), d, REMOVEFILTERS ( 'Table'[REMARKS.HEAD] ) ),
        CALCULATE ( COUNTROWS ( 'Table' ), d, REMOVEFILTERS ( 'Table'[REASON] ) )
    ) 
-- percentage of current item number of row versus parent number of rows
RETURN
    DIVIDE ( COUNTROWS ( 'Table' ), a, 0 )

 

 

 

The pattern is called "Filtering Up"  when you take an individual item and then go up one level in the hierarchy to collect all items/siblings. That is a workaround for the missing "Percentage of Parent Total"  implicit measure

lbendlin_0-1702042293972.png

One other thing worth mentioning is that we take advantage of the exit feature in SWITCH - it stops when it found a match.  We start from the lowest hierarchy level and stop once we find a level that is in scope (ie that is rendered in the matrix).



Hi @lbendlin

Thanks for your explains but only your lasted solution version(*) works in my case and I don't understand why you use these filters like:

 

, ALLSELECTED('Table')
, ALLSELECTED('Table'[DATE])

, 'Table'[REASON] = MAX('Table'[REMARKS.HEAD])
, 'Table'[REMARKS.HEAD] = MAX('Table'[REMARKS.HEAD])

 

* Your worked solution:

 

VAR _total = SWITCH(TRUE(),
    ISINSCOPE('Table'[REMARKS]),
        CALCULATE(COUNTROWS('Table')
            , ALLSELECTED('Table')
            , ALLSELECTED('Table'[DATE])
            , 'Table'[REASON] = MAX('Table'[REMARKS.HEAD])
            , 'Table'[REMARKS.HEAD] = MAX('Table'[REMARKS.HEAD])
        ),
    ISINSCOPE('Table'[REMARKS.HEAD])
        ,CALCULATE(COUNTROWS(Table')
            , ALLSELECTED('Table')
            , ALLSELECTED('Table'[DATE])
            , 'M EMC Details'[REASON]= MAX('Table'[REASON])
        ),
    CALCULATE(COUNTROWS('Table')
        , ALLSELECTED('Table')
            , ALLSELECTED('Table'[DATE])
        )
    )

RETURN  DIVIDE(COUNTROWS('Table'), _total, 0)

 

Many thanks to you

You modified my version in a way that I don't agree with. Don't use multiple ALLSELECTED. Don't run multiple identical calculations (use variables instead).

 

PLease use this version

InCategory =
-- grab the date range for the entire table so we can reapply it for the individual items
VAR d =
    ALLSELECTED ( 'Table'[DATE] ) 
-- get the total number of entries for all siblings in the current scope level
VAR a =
    SWITCH (
        TRUE (),
        ISINSCOPE ( 'Table'[REMARKS] ), CALCULATE ( COUNTROWS ( 'Table' ), d, REMOVEFILTERS ( 'Table'[REMARKS] ) ),
        ISINSCOPE ( 'Table'[REMARKS.HEAD] ), CALCULATE ( COUNTROWS ( 'Table' ), d, REMOVEFILTERS ( 'Table'[REMARKS.HEAD] ) ),
        CALCULATE ( COUNTROWS ( 'Table' ), d, REMOVEFILTERS ( 'Table'[REASON] ) )
    ) 
-- percentage of current item number of row versus parent number of rows
RETURN
    DIVIDE ( COUNTROWS ( 'Table' ), a, 0 )

 

Thank you @lbendlin 

That is the long journey and you help me a lot to understand this topic. Many thanks to you ❤️

Your solution is very good.

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!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.