PBI | Ranking dynamically based in different levels of product granularity - powerbi

I hope everyone that's reading this message is having an wonderful day
I'm running into a problem, that I would really appreciate your help :slightly_smiling_face:
I have a dataset in PBI, where I have to Rank per 3 different LEVELS of granularity (Cat1, Cat2, Cat3).
WHAT I WANT?
Sales Rank per Category 1 and Brand:
I want to rank by the total Sales (Brand in Category 1) / Total Sales (Cat1)
Sales Rank per Category 2 and Brand:
I want to rank by the total Sales (Brand in Category 2) / Total Sales (Cat2)
Sales Rank per Category 3 and Brand:
I want to rank by the total Sales (Brand in Category 3) / Total Sales (Cat3)
Explaining further:
Example 1:
Having Category 1 filtered, Category 2 filtered and Category 3 filtered I want to:
Rank by the Sales of each Brand in each Category 3 Item / Total Sales(Cat3 item)
Columns Cat1, Cat2 and Cat3 are filtered in the Matrix
Company ABC has 1000$ in Sales in the segment from Cat1 Beauty and
segment from Cat2 Body and segment from Cat3 Body Milk
Company XYZ has 800$ in Sales in the segment from Cat1 Beauty and
segment from Cat2 Body and segment from Cat3 Body Milk
Company DZA has 200$ in Sales in the segment from Cat1 Beauty and
segment from Cat2 Body and segment from Cat3 Body Milk
Find in BOLD the expected OUTPUT
Expected Output:
Brand
Cat1
Cat2
Cat3
SalesQuantity
Sales MS %
Rank by MS %
ABC
Beauty
Body
Body Milk
1000
50%
1
XYZ
Beauty
Body
Body Milk
800
40%
2
DZA
Beauty
Body
Body Milk
200
10%
3
Example 2:
Having Category 1 filtered, Category 2 filtered and Category 3 NOT filtered I want to:
Rank by the Sales of each Brand for each Category 2 Item / Total Sales(Cat2 item)
Example:
Columns Cat1, Cat2 are filtered in the Matrix
Company ABC has 1000$ in Sales in the segment from Cat1 Beauty and
segment from Cat2 Body
Company ABC has 300$ in Sales in the segment from Cat1 Beauty and
segment from Cat2 Hair
Company XYZ has 800$ in Sales in the segment from Cat1 Beauty and
segment from Cat2 Body
Company XYZ has 500$ in Sales in the segment from Cat1 Beauty and
segment from Cat2 Hair
Company DZA has 200$ in Sales in the segment from Cat1 Beauty and
segment from Cat2 Body
Company DZA has 200$ in Sales in the segment from Cat1 Beauty and
segment from Cat2 Hair
Company DZA has 250$ in Sales in the segment from Cat1 Beauty and
segment from Cat2 Hands
Find in BOLD the expected OUTPUT
Expected Output:
Brand
Cat1
Cat2
Sales Quantity
Sales MS %
Rank by MS %
ABC
Beauty
Body
1000
50%
1
ABC
Beauty
Hair
300
30%
2
XYZ
Beauty
Body
800
40%
2
XYZ
Beauty
Hair
500
50%
1
DZA
Beauty
Body
200
10%
3
DZA
Beauty
Hair
200
20%
3
DZA
Beauty
Hands
250
100%
1
Example 3:
Having Category 1 filtered, Category 2 NOT FILTERED and Category 3 NOT FILTERED I want to:
Rank by the Sales of each Brand for each Category 1 Item / Total Sales(Cat1 item)
Example:
Company ABC has 1000$ in Sales in the segment from Cat1 Beauty
Company ABC has 300$ in Sales in the segment from Cat1 Home
Company ABC has 300$ in Sales in the segment from Cat1 Men's
Clothing
Company XYZ has 800$ in Sales in the segment from Cat1 Beauty
Company XYZ has 500$ in Sales in the segment from Cat1 Home
Company XYZ has 500$ in Sales in the segment from Cat1 Men's
Clothing
Company DZA has 200$ in Sales in the segment from Cat1 Beauty
Company DZA has 200$ in Sales in the segment from Cat1 Home
Company DZA has 200$ in Sales in the segment from Cat1 Men's
Clothing
Company DZA has 200$ in Sales in the segment from Cat1 Women's
Clothing
Find in BOLD the expected OUTPUT
Expected Output:
Brand
Cat1
Sales Quantity
Sales MS %
Rank by MS %
ABC
Beauty
1000
50%
1
ABC
Home
300
30%
2
ABC
Men's Clothing
300
30%
2
XYZ
Beauty
800
40%
2
XYZ
Home
500
50%
1
XYZ
Men's Clothing
500
50%
1
DZA
Beauty
200
10%
3
DZA
Home
200
20%
3
DZA
Men's Clothing
200
20%
3
DZA
Women's Clothing
200
100%
1
What I need from you?
The Measure Market Share Volume % is well built? I Want that the measure calculates based in the Cat filtered
MS Volume % =
DIVIDE (
CALCULATE ( SUM(FACT_SALES[SALES_QTY] )),
CALCULATE (
SUM (FACT_SALES[SALES_QTY] ),
ALLEXCEPT (DIM_PRODUCT,DIM_PRODUCT[Cat1],DIM_PRODUCT[Cat2],DIM_PRODUCT[Cat3] )
),
What could be an example formula to give me what I want? I've tried with RANKX several times, but not seeing the expected results (you can find Rank MS % Measure that i'm trying to build in the link sent)
Any other suggestion to produce a similar output but with another structure?
DUMMY DATA:
Please find in the link [PBI + Dataset] PBI + Dataset
Thank you very much
Diego

Related

How do I create a pivot table with weighted averages from a table in PowerBI?

I have data in the following format:
Building
Tenant
Type
Floor
Sq Ft
Rent
Term Length
1 Example Way
Jeff
Renewal
5
100
100
6
47 Fake Street
Tom
New
3
500
200
12
I need to create a visualisation in PowerBI that displays a pivot table of attribute by tenant, with a weighted averages (by square foot) column, like this:
Jeff
Tom
Weighted Average (by Sq Ft)
Building
1 Example Way
47 Fake Street
-
Type
Renewal
New
-
Floor
5
3
-
Sq Ft
100
500
433.3333333
Rent
100
200
183.3333333
Term Length (months)
6
12
11
I have unpivoted the original data, like this:
Tenant
Attribute
Value
Jeff
Building
1 Example Way
Jeff
Type
Renewal
Jeff
Floor
5
Jeff
Sq Ft
100
Jeff
Rent
100
Jeff
Term Length (months)
6
Tom
Building
47 Fake Street
Tom
Type
New
Tom
Floor
3
Tom
Sq Ft
500
Tom
Rent
200
Tom
Term Length (months)
12
I can almost create what I need from the unpivoted data using a matrix (as below), but I can't calculate the weighted averages column from that matrix.
Jeff
Tom
Building
1 Example Way
47 Fake Street
Type
Renewal
New
Floor
5
3
Sq Ft
100
500
Rent
100
200
Term Length (months)
6
12
I can also create a table with my attributes as headers (instead of in a column). This displays the right values and lets me calculate weighted averages (as below).
Building
Type
Floor
Sq Ft
Rent
Term Length (months)
Jeff
1 Example Way
Renewal
5
100
100
6
Tom
47 Fake Street
New
3
500
200
12
Weighted Average (by Sq Ft)
-
-
-
433.3333333
183.3333333
11
However, it's important that these values are displayed vertically instead of horizontally. This is pretty straightforward in Excel, but I can't figure out how to do it in PowerBI. I hope this is clear. Can anyone help?
Thanks!

Power BI - Showing Top 5 records in Metrix Table but total should show for all records

I have table with thousands of record. i want to create a table visual that will show top 5 records for each category. i created a measure to achieve this and i am getting the result exactly the same i am looking for but facing one issue there.
See below image where i am showing top 5 records for each category, but after each category i have total.
I don't want that total for top 5 records i am showing in the table instead i want the total of all the records which is there under each category.
How can i achieve that?
Measure I created is - Top 5 = RankX(AllSelected(table(Category), Table(account), table(name)),amount_measure,,,Dense)
for Top 5 measure i am putting the filter for top 5.
Category
Account
Name
P%
amount
country
owner
Food
A101
AA11
10%
105
India
A
Food
A102
AA12
20%
120
India
A
Food
A103
AA13
80%
100
India
A
Food
A104
AA14
30%
150
India
A
Food
A105
AA15
60%
90
India
A
Stat
B101
AA11
10%
205
India
A
Stat
B102
AA12
20%
220
India
A
Stat
B103
AA13
80%
200
India
A
Stat
B104
AA14
30%
250
India
A
Stat
B105
AA15
60%
190
India
A
Admn
D101
AD11
10%
305
India
A
Admn
D102
AD12
20%
320
India
A
Admn
D103
AD13
80%
300
India
A
Admn
D104
AD14
30%
350
India
A
Admn
D105
AD15
60%
290
India
A
Thanks,
SK
You can try this
Let's suppose you have the following measures
_sumAMT:= SUM('Table 1'[amount])
and this is your ranking measure
_sumAMTRank:= RANKX(ALLEXCEPT('Table 1','Table 1'[Category]),[_sumAMT],,DESC,Dense)
You can revise the subtotal by doing this
_sumAMT by CAT:= CALCULATE(SUM('Table 1'[amount]),ALLEXCEPT('Table 1','Table 1'[Category]))
_revisedTotal:= IF(HASONEVALUE('Table 1'[Name])=true(),[_sumAMT],[_sumAMT by CAT])

Aggregate (Mean) of a resulting visual in Power BI

I am having a problem since I want an aggregated version of a filtered resulting visual.
Process:
Using a Text Filter:
a. Input Merchant ID and Year-Month.
Resulting Visual 1 will look like this if you input Merchant ID (1234) and Year-Month (2020-01):
Merchant ID CardNum Year-Month Amount
1234 1abc1 2020-01 1.00
1234 2def2 2020-01 100.00
1234 3ghi3 2020-01 65.00
Visual 2 then displays the Merchant History for each CardNumber listed in Visual 1.
Resulting Visual 2 will look like this:
Card Number Merchant ID Date Amount
1abc1 abc 2020-01 1.00
1abc1 def 2020-01 2.00
1abc1 xyz 2020-01 3.00
2def2 abc 2020-01 100.00
2def2 xyz 2020-01 200.00
3ghi3 abc 2020-01 300.00
Now I want something that will give me this output ordered from highest to lowest:
Merchant ID xRatio
abc 1
xyz 2/3
def 1/3
xRatio is the mean of Merchant ID occurrences aggregated over the card numbers
Detailed Explanation:
Merchant ID abc occurred for all 3 card numbers thus ((1+1+1)/3) is the result
Merchant ID xyz occurred for 2 card numbers thus ((1+1)/3) is the result
Merchant ID def occurred for 1 card number only thus (1/3) is the result
where "n" is the count of unique card numbers as seen in Visual 1
Create this following 3 measure and 1 custom column in given order in your table-
1.
distinct_card_number =
CALCULATE(
DISTINCTCOUNT(your_table_name[Card Number]),
ALL(your_table_name)
)
2. Create a custom column as below
card-merchant = your_table_name[Card Number] & "-" & your_table_name[Merchant ID]
3.
distinct_merchant_count = DISTINCTCOUNT(your_table_name[card-merchant])
4. now create the final measure as below-
xRatio =
IF(
[distinct_card_number] = [distinct_merchant_count],
"1",
[distinct_merchant_count] &"/"& [distinct_card_number]
)
Now add merchant_id and xRatio to your table visual. The output will be like below-

Informatica report query

Please find the below requirement,i tried with route and a sort and aggregator but amnt getting the actual output
Source records
ID type amount desc
1 Credit 300 xyz
2 Debit 100 abc
1 Credit 400 xyz
1 Debit 500 xyz
1 Debit 200 xyz
2 Credit 200 abc
Result
1 Credit 300 xyz
1 Credit 400 xyz
1 Debit 700 ( total of 1 credit & des is null)
1 Debit 500 abc
1 Debit 200 abc
1 Credit 700
2 Credit 200 abc
2 Debit 200
2 Debit 100 abc
2 Credit 100
You could create a mapping with 2 branches:
The first branch simply passes the records without any change to data
The second branch aggregates amounts grouping by ID and Type
You can then put a Union to get all records together, then a Sorter on the same keys, ID and Type, to get data in the right order for the Target.
The mapping would look like this:
Source -> Expression -> Union -> Sorter -> Target
+-> Aggregator --^

Self Join in Pandas: Merge all rows with the equivalent multi-index

I have one dataframe in the following form:
df = pd.read_csv('data/original.csv', sep = ',', names=["Date", "Gran", "Country", "Region", "Commodity", "Type", "Price"], header=0)
I'm trying to do a self join on the index Date, Gran, Country, Region producing rows in the form of
Date, Gran, Country, Region, CommodityX, TypeX, Price X, Commodity Y, Type Y, Prixe Y, Commodity Z, Type Z, Price Z
Every row should have all the different commodities and prices of a specific region.
Is there a simple way of doing this?
Any help is much appreciated!
Note: I simplified the example by ignoring a few attributes
Input Example:
Date Country Region Commodity Price
1 03/01/2014 India Vishakhapatnam Rice 25
2 03/01/2014 India Vishakhapatnam Tomato 30
3 03/01/2014 India Vishakhapatnam Oil 50
4 03/01/2014 India Delhi Wheat 10
5 03/01/2014 India Delhi Jowar 60
6 03/01/2014 India Delhi Bajra 10
Output Example:
Date Country Region Commodit1 Price1 Commodity2 Price2 Commodity3 Price3
1 03/01/2014 India Vishakhapatnam Rice 25 Tomato 30 Oil 50
2 03/01/2014 India Delhi Wheat 10 Jowar 60 Bajra 10
What you want to do is called a reshape (specifically, from long to wide). See this answer for more information.
Unfortunately as far as I can tell pandas doesn't have a simple way to do that. I adapted the answer in the other thread to your problem:
df['idx'] = df.groupby(['Date','Country','Region']).cumcount()
df.pivot(index= ['Date','Country','Region'], columns='idx')[['Commodity','Price']]
Does that solve your problem?