Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
DeBIe
Post Partisan
Post Partisan

How to add an extra totals column to Matrix to show only the average

Hey all,

 

I have a basic matrix which is showing the total documents per month for a whole year. My customer has a new requirement. She would like to see an extra column at the end with the average. In addition to that she would like to to display the value of the current month in red IF it is below the total average value. This is what I have now:

January-23February-23March-23Total
20201050
10105070

 

This is what the end result should look like

 

January-23February-23March-23TotalTotalAvg
2020105016,6
1010507023,3

 

Thank you for your time!

1 ACCEPTED SOLUTION
v-jianboli-msft
Community Support
Community Support

Hi @DeBIe ,

 

Please try:

First create a new table and create relationship between the tables:

vjianbolimsft_0-1680512366535.png

vjianbolimsft_1-1680512459499.png

Then apply these measure as I mentioned before:

Measure = 
var _a = CALCULATE(COUNT(DocumentFinal[InvoiceID-DocType]),ALLEXCEPT(DocumentFinal,DocumentFinal[fk_company_id]))
var _b = CALCULATETABLE(SUMMARIZE('DocumentFinal','Table'[Month Name],"Value",[Total Documents Uploaded]),USERELATIONSHIP('Calendar'[Dates],DocumentFinal[CreateDate]))
var _c = COUNTROWS(_b)
return 
SWITCH(TRUE(),
ISINSCOPE('Table'[Month Name])&&MAX('Table'[Month Name])<>"Total",[Total Documents Uploaded],
ISINSCOPE('Table'[Month Name])&&MAX('Table'[Month Name])="Total",_a,
_a/_c)
Format Color =
VAR _a =
    CALCULATE (
        COUNT ( DocumentFinal[InvoiceID-DocType] ),
        ALLEXCEPT ( DocumentFinal, DocumentFinal[fk_company_id] )
    )
VAR _b =
    CALCULATETABLE (
        SUMMARIZE (
            'DocumentFinal',
            'Table'[Month Name],
            "Value", [Total Documents Uploaded]
        ),
        USERELATIONSHIP ( 'Calendar'[Dates], DocumentFinal[CreateDate] )
    )
VAR _c =
    COUNTROWS ( _b )
RETURN
    IF (
        SELECTEDVALUE ( 'Table'[Month Name] ) = FORMAT ( TODAY (), "MMMM" )
            && [Measure] < _a / _c,
        "Red",
        "Black"
    )

Output:

vjianbolimsft_2-1680515096756.png

PS: The result is calculated with the current date being March, if it is April, the result will be different.

 

Best Regards,

Jianbo Li

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

8 REPLIES 8
DeBIe
Post Partisan
Post Partisan

@v-jianboli-msft 

 

Theres one more question I would like to ask. I forgot to mention that I would like to display the Name of the company instead of the fk_company_id. The name of the company is in the related lookup table Company_Lookup.

Relation: DocumentFinal[fk_company_id] * -> 1 Company_Lookup[id]

I have modified the measure but for some reason the Total is not showing:


_Measure = 
    var _a = CALCULATE(COUNT(DocumentFinal[InvoiceID-DocType]),ALLEXCEPT(Company_Lookup,Company_Lookup[Name]))
    var _b = CALCULATETABLE(SUMMARIZE(DocumentFinal,Calendar_YearMonthName[YearMonthNumber],"Value", [Total Invoices Uploaded]),USERELATIONSHIP('Calendar'[Dates],DocumentFinal[CreateDate]))
    var _c = COUNTROWS(_b)
    return
    SWITCH(TRUE(),
    ISINSCOPE(Calendar_YearMonthName[YearMonthNumber])&&MAX(Calendar_YearMonthName[YearMonthNumber])<>"Total", [Total Invoices Uploaded],
    ISINSCOPE(Calendar_YearMonthName[YearMonthNumber])&&MAX(Calendar_YearMonthName[YearMonthNumber])="Total",_a,_a/_c)

 

The color is also not showing correct results when I modified the measure for color formatting:

_TestColor = 
VAR _a =
    CALCULATE (
        COUNT ( DocumentFinal[InvoiceID-DocType] ),
        ALLEXCEPT ( Company_Lookup,Company_Lookup[Name] )
    )
VAR _b =
    CALCULATETABLE (
        SUMMARIZE (
            DocumentFinal,
            Calendar_YearMonthName[YearMonthNumber],
            "Value", [Total Invoices Uploaded]
        ),
        USERELATIONSHIP ( 'Calendar'[Dates], DocumentFinal[CreateDate] )
    )
VAR _c =
    COUNTROWS ( _b )
RETURN
    IF (
        SELECTEDVALUE ( Calendar_YearMonthName[YearMonthNumber] ) = FORMAT ( TODAY (), "YYYYMM" )
            && [_MeasureBookbot] < _a / _c,
        "Red",
        "Black"
    )

 

DeBIe_0-1681202458201.png

 

@v-jianboli-msft 

 

If you have time, can you please help me with finishing this requirement?

 

Thanks a lot.

v-jianboli-msft
Community Support
Community Support

Hi @DeBIe ,

 

Please try:

First create a new table and create relationship between the tables:

vjianbolimsft_0-1680512366535.png

vjianbolimsft_1-1680512459499.png

Then apply these measure as I mentioned before:

Measure = 
var _a = CALCULATE(COUNT(DocumentFinal[InvoiceID-DocType]),ALLEXCEPT(DocumentFinal,DocumentFinal[fk_company_id]))
var _b = CALCULATETABLE(SUMMARIZE('DocumentFinal','Table'[Month Name],"Value",[Total Documents Uploaded]),USERELATIONSHIP('Calendar'[Dates],DocumentFinal[CreateDate]))
var _c = COUNTROWS(_b)
return 
SWITCH(TRUE(),
ISINSCOPE('Table'[Month Name])&&MAX('Table'[Month Name])<>"Total",[Total Documents Uploaded],
ISINSCOPE('Table'[Month Name])&&MAX('Table'[Month Name])="Total",_a,
_a/_c)
Format Color =
VAR _a =
    CALCULATE (
        COUNT ( DocumentFinal[InvoiceID-DocType] ),
        ALLEXCEPT ( DocumentFinal, DocumentFinal[fk_company_id] )
    )
VAR _b =
    CALCULATETABLE (
        SUMMARIZE (
            'DocumentFinal',
            'Table'[Month Name],
            "Value", [Total Documents Uploaded]
        ),
        USERELATIONSHIP ( 'Calendar'[Dates], DocumentFinal[CreateDate] )
    )
VAR _c =
    COUNTROWS ( _b )
RETURN
    IF (
        SELECTEDVALUE ( 'Table'[Month Name] ) = FORMAT ( TODAY (), "MMMM" )
            && [Measure] < _a / _c,
        "Red",
        "Black"
    )

Output:

vjianbolimsft_2-1680515096756.png

PS: The result is calculated with the current date being March, if it is April, the result will be different.

 

Best Regards,

Jianbo Li

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

Thank you very much @v-jianboli-msft, this works!

v-jianboli-msft
Community Support
Community Support

Hi @DeBIe ,

 

Could you tell me what your 'Calendar' [Dates] looks like? From your description, it seems to have an inactive relationship with DocumentFinal[CreateDate]. If this is the case, the columns of your matrix visual will be date by date and average will be calculated based on dates, not months.

Please provide me with more details about your table and your problem or share me with your pbix file after removing sensitive data.

 

Refer to:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

 

Best Regards,

Jianbo Li

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

Hi @v-jianboli-msft ,

 

Sorry for my late response. I did not see that you had send a reply :(. 

 

I've set up a pbix.file. Can you have a look? I did not find the option to upload the pbix. file here, so I uploaded it to wetransfer.
https://we.tl/t-BeK9C8z4q2 

 

In most of my report I am using the active relationships between [DocumentFinal](Bookdate) -> [Calendar](Dates). However, sometimes I need to use the inactive relationship [DocumentFinal](CreateDate) -> [Calendar](Dates).

 

Thank you very much for your time again.

 

 

v-jianboli-msft
Community Support
Community Support

Hi @DeBIe ,

 

Baseed on your description, I have created a simple sample:

vjianbolimsft_0-1679536818789.png

Pleass try:

First create a new table and create relationship between the tables:

Table 2 = UNION(SUMMARIZE('Table','Table'[YearMonth]),{"Total"})

vjianbolimsft_1-1679536916130.png

vjianbolimsft_2-1679536936886.png

Then apply the measure to the matrix visual:

Measure = SWITCH(TRUE(),
ISINSCOPE('Table 2'[YearMonth])&&MAX('Table 2'[YearMonth])<>"Total",SUM('Table'[Value]),
ISINSCOPE('Table 2'[YearMonth])&&MAX('Table 2'[YearMonth])="Total",CALCULATE(SUM('Table'[Value]),ALLEXCEPT('Table','Table'[ID])),
AVERAGEX(SUMMARIZE('Table','Table'[YearMonth],"Value",SUM('Table'[Value])),[Value])
)

Rename column subtotals:

vjianbolimsft_3-1679537024031.png

Output:

vjianbolimsft_4-1679537059012.png

Then use this measrue to conditional formatting the value:

Format Color =
IF (
    MAX ( 'Table'[YearMonth] ) = FORMAT ( TODAY (), "MMM-YY" )
        && [Measure]
            < AVERAGEX (
                SUMMARIZE (
                    ALLEXCEPT ( 'Table', 'Table'[ID] ),
                    'Table'[YearMonth],
                    "Value", SUM ( 'Table'[Value] )
                ),
                [Value]
            ),
    "Red",
    "Black"
)

vjianbolimsft_5-1679537160574.png

vjianbolimsft_6-1679537181245.png

Final output:

vjianbolimsft_7-1679537195072.png

Best Regards,

Jianbo Li

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

Hi @v-jianboli-msft ,

 

Thank you so much for the suggested solution. From your example I can see that it is exactly what I need. I was hoping to achieve it in my model but my model is set up a bit different than I presented (sorry for that). However I don't think it changes a lot in the formula, but I don't know how to get the last part of the formula correct for the Averagex function. 

This is how my data really looks like:

 

DocumentIDCompanyIDCreateDate
19911-1-2023
26522-1-2023
33783-1-2023
48994-1-2023
59515-1-2023
69511-2-2023
76502-2-2023
83313-2-2023
95884-2-2023
104175-2-2023
114616-2-2023
124211-3-2023
133812-3-2023
149513-3-2023
153784-3-2023
162615-3-2023
172216-3-2023
183787-3-2023
191418-3-2023
209519-3-2023


Good to know is that the CreateDate is a inactive relationship in my model. To calculate the Total documents per month I am using this formula:

Total Documents Uploaded = CALCULATE(COUNT(DocumentFinal[DocumentID]),USERELATIONSHIP(DocumentFinal[CreateDate],'Calendar'[Dates]))

 

This is how far I've got based on your suggested measure:

DocumentsUploadedSolution = SWITCH(TRUE(),
    ISINSCOPE('Calendar'[Dates])&&MAX('Calendar'[Dates])<>"Total", CALCULATE(COUNT(DocumentFinal[DocumentID]),USERELATIONSHIP(DocumentFinal[CreateDate],'Calendar'[Dates]),
    ISINSCOPE('Calendar'[Dates])&&MAX('Calendar'[Dates])="Total",CALCULATE(COUNT(DocumentFinal[DocumentID]),USERELATIONSHIP(DocumentFinal[CreateDate],'Calendar'[Dates]),ALLEXCEPT(DocumentFinal,DocumentFinal[fk_company_id]))),
    AVERAGEX(SUMMARIZE(DocumentFinal,DocumentFinal[CreateDate],"Value",CALCULATE(COUNT(DocumentFinal[DocumentID]),USERELATIONSHIP(DocumentFinal[CreateDate],'Calendar'[Dates])))))
 
I think we're almost there. Thank you so much for your time. Greatly appreciated.

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.