Топ N по категориям - PullRequest
       122

Топ N по категориям

1 голос
/ 07 августа 2020

Я работаю над расчетом ниже; Я пробовал разные решения, но не смог получить требуемый результат. Примерная таблица выглядит так, как показано ниже, с идентификатором, количеством, категорией, типом;

enter image description here

I have a pie chart that shows the split of cash and card. Also, I created a measure to identify the average amount by each category like below.

AverageExpenseType = 
    AVERAGEX(
        SUMMARIZE('Data',
            'Data'[Category],
            "Total Spent", AVERAGE('Data'[Amount])),
        [Total Spent])

What I need to create is a table where the Top 5 ID's amount is more than the average to the respective Category and Type. i.e., when I select Cash in the pie chart, the average needs to calculated based on the cash or card as well.

For eg. the average of category A for cash is 4710 and the output looks like below where the ID having more than the average for Category A and Cash type

введите описание изображения здесь

1 Ответ

1 голос
/ 12 августа 2020

вы можете создать настраиваемый столбец в своей таблице с помощью этого кода ниже -

category_type_wise_average = 

VAR current_category = pie_chart[category]
VAR current_type = pie_chart[type]

VAR category_wise_average = 
AVERAGEX(
    FILTER(
        pie_chart,
        pie_chart[category] = current_category
            && pie_chart[type] = current_type
    ),
    pie_chart[amount]
)

RETURN category_wise_average

Теперь у вас будет среднее значение категории и типа в каждой строке, как показано на изображении ниже. Теперь вы можете применить необходимую фильтрацию в соответствии с вашими требованиями.

enter image description here

SOLUTION 2: Now, if you feel there is performance issue in generation a custom column with average calculation for every row, you can try this below option with same result-

Step-1: Create a new custom table with this below code-

pie_group_average = 

VAR sub_category_wise_total = 
GROUPBY (  
    pie_chart,
    pie_chart[category],
    pie_chart[type],
    "average",AVERAGEX(CURRENTGROUP(), pie_chart[amount])    
) 

RETURN
SELECTCOLUMNS (
    sub_category_wise_total,
    "category",pie_chart[category],
    "type",pie_chart[type],
    "average",[average] 
)

Step-2: Now create a Custom Column in your main table as below-

category_type_wise_average_2 = LOOKUPVALUE(
    pie_group_average[average],   
    pie_group_average[category],pie_chart[category],
    pie_group_average[type],pie_chart[type]
) 

This measure will now hold the same value we generated in solution-1 with custom column.

Additional Steps:

Step-3: Please create another custom column as-

show_hide = 
IF(
    pie_chart[amount] >= pie_chart[category_type_wise_average_2], 
    1, 
    0
)

Step-4: Add these 2 visual level filter as shown in the below image-

enter image description here

Step by step change in data showing will be as below-

введите описание изображения здесь

Надеюсь, это вам поможет!

...