Forum Discussion

lynnpowerbi's avatar
lynnpowerbi
Frequent Visitor
3 years ago
Solved

Sort by Top N in bar chart with Multiple Columns

Hi,

 

I am currently working with a dataset that looks like the following:

 

 

Case ID   ProcessOutcomeDateTime    Error1.1    Error1.2   Error 2.1  Error2.2
Case1     01/01/23                      1          0         1           0
Case1     02/01/23                      1          0         1           0
Case2     3/02/23                       0          0         1           0
Case2     4/02/23                       0          0         1           0

 

Note: There are duplicate CaseIDs in this table, however I cannot simply remove them as they carry other attributes that I use for calculcations. 

 

I have then created a dimension table to group these errors into categories to look like the following - 

 

 

Case ID   Min ProcessOutcomeDateTime   Total Errors  Error1   Error2
Case1     01/01/23                          2          1        1
Case2     3/02/23                           1          0        1

 

 

The way in which Total Errors and Error Categories are calculated are based on the Minimum ProcessOutcomeDateTime of each Case ID. 

 

I then realised that I had to create measures to allow these to be filtered on in visualisations, so I created Error Category Measures such as this:

 

 

Error Category 7 Measure = 

VAR caseidvar = 
VALUES('DimensionTable'[caseid])

VAR FirstProcessOutcomedate =
VALUES('DimensionTable'[MinProcessOutcomedate])

RETURN


CALCULATE(
    SUM(factPreDecisionErrors[7.01])
    + SUM(factPreDecisionErrors[7.02])
    + SUM(factPreDecisionErrors[7.03])
    +SUM(factPreDecisionErrors[7.04])
    + SUM(factPreDecisionErrors[7.05])
    + SUM(factPreDecisionErrors[7.06])
  ),
  'FactTable'[caseid] IN caseidvar
    && 'FactTable'[ProcessOutcomedate] IN firstprocessoutcomedate,
  CROSSFILTER('DimensionTable'[caseid], FactTable[caseid], Both) 
) 

 

 

What I am trying to achieve is a horizontal bar chart on Top Error Categories based on the Top N of Errors.

Eg if Error Category 8 and 9 had the highest number of errors, this will show up at the top with the longest bars.

 

I understand that best practice is to Unpivot columns on my Fact Table, but because I cannot remove duplicate Case IDs and as each case ID can hold multiple error codes & categories, I am unable to do this.

 

Your help would be massively appreciated

 

Thank you

 

 

 

 

  • Anonymous's avatar
    Anonymous
    3 years ago

    HI lynnpowerbi,

    In fact, power bi does not suitable to calculate across multiple fields, I'd like to suggest you do unpivot column on these value fields, to convert them to attribute and value. Then you can use unpivoted attribute and value fields to accurately control and calculate on these field values based on simple conditions.

    Unpivot columns - Power Query | Microsoft Learn

    For sorting with multiple columns, you can also take a look at the following link to use ISONORAFTER function to write complex condition with multiple fields.      

    Sorting by multiple columns 

    Regards,

    Xiaoxin Sheng

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    HI lynnpowerbi,

    In fact, power bi does not suitable to calculate across multiple fields, I'd like to suggest you do unpivot column on these value fields, to convert them to attribute and value. Then you can use unpivoted attribute and value fields to accurately control and calculate on these field values based on simple conditions.

    Unpivot columns - Power Query | Microsoft Learn

    For sorting with multiple columns, you can also take a look at the following link to use ISONORAFTER function to write complex condition with multiple fields.      

    Sorting by multiple columns 

    Regards,

    Xiaoxin Sheng