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
TotoBlank
Frequent Visitor

Histogram : Compare Category with subcategory

Hello,

 

I want to build a report in PBI, that shows me the following information in a histogram:

 

- Axe X : Category, Subcategory

- Axe Y : Total amount

- Filter : Subcategory + Date

 

 

Is't possible to build this report ?

TotoBlank_0-1673859174713.png

 

Thanks in advance.

1 ACCEPTED SOLUTION

Hi, @TotoBlank 

Thanks for your response and detailed description for your question. I'm sorry i may make a mistake in this dax.

You can try to use dax to test:

Measure = var _flag= MAX('X Table'[Flag])
var _name = MAX('X Table'[Column1])
var _sub = CALCULATE( SUM('Table'[Amount]) ,KEEPFILTERS( TREATAS({_name} , 'Table'[Subcategory])))
var _cat = CALCULATE( SUM('Table'[Amount]) , TREATAS({_name} , 'Table'[Category]),ALL('Table'[Subcategory]) )
var _cat_table = VALUES('Table'[Category])
return IF(_flag = "CAT" && _name in _cat_table,_cat ,_sub)

Then we can meet your need:

 

vyueyunzhmsft_0-1674003736274.png

Best Regards,

Aniya Zhang

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

4 REPLIES 4
v-yueyunzh-msft
Community Support
Community Support

Hi , @TotoBlank 

Accoridng to your description, you want to show both cat and sub-cat in the visual.

Here are the steps you can refer to :
(1)This is my test data:

vyueyunzhmsft_0-1673926262551.png

(2)We need to create a x-axis table in Power Query Editor, we can create a blank query in it and enter this:

vyueyunzhmsft_2-1673926303636.png

= Table.AddIndexColumn(Table.AddColumn(Table.FromColumns({List.Distinct(Table[Subcategory])}),"Flag",(x)=>"SUB") & Table.AddColumn(Table.FromColumns({List.Distinct(Table[Category])}),"Flag",(x)=>"CAT"), "Index", 1, 1, Int64.Type)

(3)Then we can apply the data to Desktop and we can create a measure:

Measure = var _flag=  MAX('X Table'[Flag])
var _name = MAX('X Table'[Column1])
var _sub = CALCULATE( SUM('Table'[Amount]) , TREATAS({_name} , 'Table'[Subcategory]))
var _cat  =  CALCULATE( SUM('Table'[Amount]) , TREATAS({_name} , 'Table'[Category]))
return
IF(_flag = "CAT",_cat ,_sub)

(4) Then we can put the fields on the visual and we can meet your need , the result is as follows:

vyueyunzhmsft_3-1673926363325.png

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Hi @v-yueyunzh-msft ,

 

Thanks for the comment.

 

What I really need is to compare a subcategory to its category. If I take your example :

 

My Data:

TotoBlank_0-1673958528571.png

We need to choose date and Subcatory, so we have 2 filters :

TotoBlank_1-1673958643896.png

If i select 01/01/2023, this is the result :

TotoBlank_2-1673958787636.png

we have 2 Subcategories ( and 2 categories). But when i selecte Subcategory S1, we shold to have this result without S10 :

TotoBlank_3-1673958981965.png

The result of table is corect but not the report.

Thanks
Best Regards

 

 

Hi, @TotoBlank 

Thanks for your response and detailed description for your question. I'm sorry i may make a mistake in this dax.

You can try to use dax to test:

Measure = var _flag= MAX('X Table'[Flag])
var _name = MAX('X Table'[Column1])
var _sub = CALCULATE( SUM('Table'[Amount]) ,KEEPFILTERS( TREATAS({_name} , 'Table'[Subcategory])))
var _cat = CALCULATE( SUM('Table'[Amount]) , TREATAS({_name} , 'Table'[Category]),ALL('Table'[Subcategory]) )
var _cat_table = VALUES('Table'[Category])
return IF(_flag = "CAT" && _name in _cat_table,_cat ,_sub)

Then we can meet your need:

 

vyueyunzhmsft_0-1674003736274.png

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

hi @v-yueyunzh-msft ,

 

Thanks for your response. I'll try it tomorrow.

 

Do you know if is't possible to do this ? Compare periods: same month in 3 different years - Microsoft Power BI Community

 

Thanks

Best Regards,

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.