skip to main content
Power BI
    • What is Power BI
    • Why Power BI
    • Customer stories
    • Data visuals
    • Security
    • Power BI Desktop
    • Power BI Pro
    • Power BI Premium
    • Power BI Mobile
    • Power BI Embedded
    • Power BI Report Server
  • Pricing
    • Azure + Power BI
    • Microsoft 365 + Power BI
    • Dynamics 365 + Power BI
      • Energy
      • Healthcare
      • Manufacturing
      • Media
      • Retail
    • For analysts
    • For IT
      • Overview
      • Embedded analytics
      • Power BI visuals
      • Automation
      • Documentation
      • Community
    • Getting started
      • Overview
      • Self-guided learning
      • Webinars
      • Documentation
      • Roadmap
      • Overview
      • Issues
      • Give feedback
    • Blog
    • Business intelligence topics
    • Overview
    • Forums
    • Galleries
    • Submit ideas
    • Events
    • User groups
    • Community blog
    • Register
    • ·
    • Sign in
    • ·
    • Help
    Go To
    • Microsoft Power BI Community
    • Welcome to the Community!
    • News & Announcements
    • Get Help with Power BI
    • Desktop
    • Service
    • Report Server
    • Power Query
    • Mobile Apps
    • Developer
    • DAX Commands and Tips
    • Custom Visuals Development Discussion
    • Health and Life Sciences
    • Power BI Spanish Community
    • Translated Spanish Desktop
    • Power Platform Integration - Better Together!
    • Power Platform Integrations
    • Power Platform and Dynamics 365 Integrations
    • Training and Consulting
    • Instructor Led Training
    • Galleries
    • Community Connections & How-To Videos
    • COVID-19 Data Stories Gallery
    • Themes Gallery
    • Data Stories Gallery
    • R Script Showcase
    • Webinars and Video Gallery
    • Quick Measures Gallery
    • 2021 MSBizAppsSummit Gallery
    • 2020 MSBizAppsSummit Gallery
    • 2019 MSBizAppsSummit Gallery
    • Events
    • Ideas
    • Custom Visuals Ideas
    • Issues
    • Issues
    • Events
    • Upcoming Events
    • Community Engagement
    • T-Shirt Design Challenge 2023
    • Community Blog
    • Power BI Community Blog
    • Custom Visuals Community Blog
    • Community Support
    • Community Accounts & Registration
    • Using the Community
    • Community Feedback
    cancel
    Turn on suggestions
    Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
    Showing results for 
    Search instead for 
    Did you mean: 
    • Microsoft Power BI Community
    • Galleries
    • Quick Measures Gallery
    • The New Hotness (Custom Matrix Hierarchy)

    The New Hotness (Custom Matrix Hierarchy)

    03-08-2020 09:42 AM - last edited 03-22-2020 14:40 PM

    Super User Greg_Deckler
    Super User
    16720 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    Greg_Deckler
    Super User Greg_Deckler
    Super User
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    The New Hotness (Custom Matrix Hierarchy)

    ‎03-08-2020 09:42 AM

    OK, this one is a bit complex and is in many ways a different spin on the Disconnected Table Trick.

    So, this recipe is designed to fix the issue where you have a matrix with a column or two in it and you just want to add a value onto the the end of the matrix but then the matrix decides to spew out the value for every column in the matrix and you end up trying to shrink the offending columns down so that you don't really see them, but it is tedious and ugly and generally ends up looking pretty terrible and so you are like "Ah man, this sucks, my report was looking great but now I have this jankey matrix visual that looks terrible" and you think to yourself "If only there was a way to tack columns on to the end of the matrix sort of like Totals" so then you go look in the Subtotals and Grand Totals areas of the formatting pane but you don't find anything and you realize that is probably because these extra columns wouldn't really be totals per se but since they are columns you think "Yeah, maybe in the Column headers area" but you don't find anything there either but you do notice the Grid area right above it so you check that out because, you know, what the heck, but there's nothing really helpful there so in desparation you check the Values area and still no joy and you think "Hey maybe if I click on the little drop down arrow on the column in the Values area maybe there is an option like show only once" so you check that and spend a couple hours playing around with Conditional formatting, which you had never really noticed before but then you get back to the problem at hand and you are left frustrated, depressed and all downtroddin and stuff thinking, "Yo dawg, if I could just have some option somewhere where I could just tack a few extra columns on to the end of the matrix just like I can keep tacking words on to the end of a sentence..." Bummer.

    So if you have ever found yourself in that situation. I mean, not that EXACT situation because that is perhaps oddly specific. But, you know, generally that situation. You can do this, create your own custom hierarchy like this:

     

    Custom Hierarchy = 
        { 
            ("2019", "January", 1),
            ("2019", "February", 2),
            ("2019", "March", 3),
            ("2019", "April", 4),
            ("2019", "May", 5),
            ("2019", "June", 6),
            ("2019", "July", 7),
            ("2019", "August", 8),
            ("2019", "September", 9),
            ("2019", "October", 10),
            ("2019", "November", 11),
            ("2019", "December", 12),
            ("2020", "January", 1),
            ("2020", "February", 2),
            ("2020", "March", 3),
            ("2020", "April", 4),
            ("2020", "May", 5),
            ("2020", "June", 6),
            ("2020", "July", 7),
            ("2020", "August", 8),
            ("2020", "September", 9),
            ("2020", "October", 10),
            ("2020", "November", 11),
            ("2020", "December", 12),
            ("Other", "LY", 13),
            ("Other", "PY", 14)
        }

     

     

     

    And then create a corresponding measure like this:

     

     

     

    Measure = 
        VAR __Value1 = MAX('Custom Hierarchy'[Value1])
        VAR __Value2 = MAX('Custom Hierarchy'[Value2])
    RETURN
        SWITCH(
            TRUE(),
            ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "January",
                VAR __Year = VALUE(MAX('Custom Hierarchy'[Value1]))
                VAR __Month = 1
                RETURN
                    SUMX(FILTER('Table',YEAR('Table'[Invoice Date]) = __Year && MONTH('Table'[Invoice Date]) = __Month),[Price]),
            ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "February",
                VAR __Year = VALUE(MAX('Custom Hierarchy'[Value1]))
                VAR __Month = 2
                RETURN
                    SUMX(FILTER('Table',YEAR('Table'[Invoice Date]) = __Year && MONTH('Table'[Invoice Date]) = __Month),[Price]),
            ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "March",
                VAR __Year = VALUE(MAX('Custom Hierarchy'[Value1]))
                VAR __Month = 3
                RETURN
                    SUMX(FILTER('Table',YEAR('Table'[Invoice Date]) = __Year && MONTH('Table'[Invoice Date]) = __Month),[Price]),
            ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "April",
                VAR __Year = VALUE(MAX('Custom Hierarchy'[Value1]))
                VAR __Month = 4
                RETURN
                    SUMX(FILTER('Table',YEAR('Table'[Invoice Date]) = __Year && MONTH('Table'[Invoice Date]) = __Month),[Price]),
           ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "May",
                VAR __Year = VALUE(MAX('Custom Hierarchy'[Value1]))
                VAR __Month = 5
                RETURN
                    SUMX(FILTER('Table',YEAR('Table'[Invoice Date]) = __Year && MONTH('Table'[Invoice Date]) = __Month),[Price]),
            ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "June",
                VAR __Year = VALUE(MAX('Custom Hierarchy'[Value1]))
                VAR __Month = 6
                RETURN
                    SUMX(FILTER('Table',YEAR('Table'[Invoice Date]) = __Year && MONTH('Table'[Invoice Date]) = __Month),[Price]),
            ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "July",
                VAR __Year = VALUE(MAX('Custom Hierarchy'[Value1]))
                VAR __Month = 7
                RETURN
                    SUMX(FILTER('Table',YEAR('Table'[Invoice Date]) = __Year && MONTH('Table'[Invoice Date]) = __Month),[Price]),
            ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "August",
                VAR __Year = VALUE(MAX('Custom Hierarchy'[Value1]))
                VAR __Month = 8
                RETURN
                    SUMX(FILTER('Table',YEAR('Table'[Invoice Date]) = __Year && MONTH('Table'[Invoice Date]) = __Month),[Price]),
            ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "September",
                VAR __Year = VALUE(MAX('Custom Hierarchy'[Value1]))
                VAR __Month = 9
                RETURN
                    SUMX(FILTER('Table',YEAR('Table'[Invoice Date]) = __Year && MONTH('Table'[Invoice Date]) = __Month),[Price]),
           ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "October",
                VAR __Year = VALUE(MAX('Custom Hierarchy'[Value1]))
                VAR __Month = 10
                RETURN
                    SUMX(FILTER('Table',YEAR('Table'[Invoice Date]) = __Year && MONTH('Table'[Invoice Date]) = __Month),[Price]),
            ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "November",
                VAR __Year = VALUE(MAX('Custom Hierarchy'[Value1]))
                VAR __Month = 11
                RETURN
                    SUMX(FILTER('Table',YEAR('Table'[Invoice Date]) = __Year && MONTH('Table'[Invoice Date]) = __Month),[Price]),
            ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "December",
                VAR __Year = VALUE(MAX('Custom Hierarchy'[Value1]))
                VAR __Month = 12
                RETURN
                    SUMX(FILTER('Table',YEAR('Table'[Invoice Date]) = __Year && MONTH('Table'[Invoice Date]) = __Month),[Price]), 
            ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "LY",1000,
            ISINSCOPE('Custom Hierarchy'[Value2]) && __Value2 = "PY",100,
            ISINSCOPE('Custom Hierarchy'[Value2]), BLANK(),
            ISINSCOPE('Custom Hierarchy'[Value1]) && __Value1 <> "Other",
                VAR __Year = VALUE(MAX('Custom Hierarchy'[Value1]))
                RETURN
                    SUMX(FILTER('Table',YEAR('Table'[Invoice Date]) = __Year),[Price]),
            ISINSCOPE('Custom Hierarchy'[Value1]) && __Value1 <> "Other",
                BLANK(),
            BLANK()
        )

     

     

     

     

     

     

    eyJrIjoiMDllYzdkMjEtNjdlOS00OWQ1LWE0N2YtMmEwMGRiN2JjOTg4IiwidCI6IjRhMDQyNzQzLTM3M2EtNDNkMi04MjdiLTAwM2Y0YzdiYTFlNSIsImMiOjN9


    @ me in replies or I'll lose your thread!!!
    Instead of a Kudo, please vote for this idea
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Mastering Power BI 2nd Edition

    DAX is easy, CALCULATE makes DAX hard...
    Preview file
    51 KB
    Impossible.pbix
    Labels:
    • Labels:
    • Other
    Message 1 of 12
    16,720 Views
    7
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic
    Bilhan
    Bilhan Resolver I
    Resolver I
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎09-05-2023 07:33 AM

    Hey @Greg_Deckler 

    I have a question, is this something also related to the measures we have in values bucket but we wanted to add individual measure not getting affected by the column bucket layer ?


    Message 12 of 12
    604 Views
    0
    Reply
    Aurelio
    Aurelio
    Frequent Visitor
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎12-01-2022 06:32 AM

    @Greg_Deckler Interesting look at a very annoying problem. The solution works fine but is there a way to insert additional colums after every year? 

    Message 10 of 12
    5,700 Views
    0
    Reply
    Greg_Deckler
    Super User Greg_Deckler
    Super User
    In response to Aurelio
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎12-01-2022 08:25 AM

    @Aurelio I don't see why not.


    @ me in replies or I'll lose your thread!!!
    Instead of a Kudo, please vote for this idea
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Mastering Power BI 2nd Edition

    DAX is easy, CALCULATE makes DAX hard...
    Message 11 of 12
    5,683 Views
    0
    Reply
    Anonymous
    Not applicable
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎09-16-2021 02:24 AM

    Thanks Greg for this solution, it is helping me immensely. I have customized it to fit my needs but struggle with column subtotals. It seems that the matrix cannot compute them for the additional values - LY and PY in your example, and gives blank values. For row subtotals instead it works perfectly. Any idea on what may cause this behavior? 

    Message 6 of 12
    12,156 Views
    0
    Reply
    Greg_Deckler
    Super User Greg_Deckler
    Super User
    In response to Anonymous
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎09-16-2021 02:36 AM

    @Anonymous If I understand the question correctly, once you go this route I believe you would need to add the Total columns to your hierarchy essentially and compute them yourself. If you had sample data and expected output I might be able to be more specific.


    @ me in replies or I'll lose your thread!!!
    Instead of a Kudo, please vote for this idea
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Mastering Power BI 2nd Edition

    DAX is easy, CALCULATE makes DAX hard...
    Message 7 of 12
    12,148 Views
    1
    Reply
    Anonymous
    Not applicable
    In response to Greg_Deckler
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎09-16-2021 04:24 AM

    @Greg_Deckler unfortunately i cannot provide sample data. Using your example, just imagine that LY and PY are measures that can be filtered by the rows. I would then like to have a column total which sums the monthly data with LY and PY. 

    Just to make sure I understand your suggestion, you are saying to create a further column in the measure which sums up the data, insted of relying on the built in column subtotal of matrix visual, is that correct? Thanks for your help

    Message 8 of 12
    12,132 Views
    0
    Reply
    Greg_Deckler
    Super User Greg_Deckler
    Super User
    In response to Anonymous
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎09-16-2021 04:39 AM

    @Anonymous Correct, that's what I'm thinking. 


    @ me in replies or I'll lose your thread!!!
    Instead of a Kudo, please vote for this idea
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Mastering Power BI 2nd Edition

    DAX is easy, CALCULATE makes DAX hard...
    Message 9 of 12
    12,124 Views
    1
    Reply
    Anonymous
    Not applicable
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎02-04-2021 07:12 PM

    I love this solution. Do you know if it is possible to conditionally format specific columns e.g. Ly, PY? 

    Message 4 of 12
    14,674 Views
    0
    Reply
    Anonymous
    Not applicable
    In response to Anonymous
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎07-05-2022 08:54 PM

    Hi ceyres,

     

    Did you find the way how to add conditional format using this way?

    Message 5 of 12
    7,932 Views
    0
    Reply
    CNENFRNL
    Super User CNENFRNL
    Super User
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎08-30-2020 01:14 PM

    Another piece from your colleciton of ingenious works!

    Please produce Customer Hierarchy table in PQ, if you don't mind.😉

    let
        Source = Table.FromRecords(
            List.Generate(
                () => {Date.StartOfYear(Date.AddYears(DateTime.LocalNow(), -1))},
                each Date.Year(_{0}) <= Date.Year(Date.AddYears(DateTime.LocalNow(), 0)),
                each {Date.AddMonths(_{0}, 1)},
                each [Year = Date.Year(_{0}), Month = Date.Month(_{0}), MonthName = Date.MonthName(_{0})]
            )
        )
        &
        #table({"Year", "Month", "MonthName"}, {{"Other", "LY", 13}, {"Other", "PY", 14}})
    in
        Source

     


    Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

    DAX is simple, but NOT EASY!

    Message 2 of 12
    15,903 Views
    2
    Reply
    Greg_Deckler
    Super User Greg_Deckler
    Super User
    In response to CNENFRNL
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎08-30-2020 01:45 PM

    @CNENFRNL - Oh yeah, I like that! Very nice! I wish my Power Query fu was stronger sometimes! 🙂


    @ me in replies or I'll lose your thread!!!
    Instead of a Kudo, please vote for this idea
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Mastering Power BI 2nd Edition

    DAX is easy, CALCULATE makes DAX hard...
    Message 3 of 12
    15,898 Views
    0
    Reply

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • Power Pages
    • Power Automate
    • Power Virtual Agents

    • Sign in
    • Sign up

    Browse

    • Solutions
    • Consulting Services

    Downloads

    • Power BI Desktop
    • Power BI Mobile
    • Power BI Report Server
    • See all downloads

    Learn

    • Guided learning
    • Documentation
    • Support
    • Community
    • Give feedback
    • Webinars
    • Developers
    • Blog
    • Newsletter

    © 2023 Microsoft

    Follow Power BI

    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Trademarks
    Consumer Privacy Act (CCPA) Opt-Out Icon Your Privacy Choices