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
usomaraju
Helper II
Helper II

Get each month total and average based on date

hello All,

 

I need help on calculating the monthly total and average for the data.

i have the data like below

IDDateDays
1632/28/2020 7:373
1642/25/2020 12:470
1653/2/2020 15:435
1663/3/2020 4:096
1733/3/2020 12:350
1743/19/2020 19:4616
1753/19/2020 19:5016
1783/4/2020 14:270
1793/4/2020 14:340
1803/4/2020 14:360
1813/4/2020 15:592
1823/4/2020 15:598
1853/10/2020 14:084
1863/10/2020 14:084
1873/10/2020 14:510
1883/10/2020 14:533
1893/11/2020 16:333
1903/11/2020 16:410
1913/12/2020 8:000
1923/12/2020 8:010
1953/12/2020 16:581
1963/12/2020 17:011
1973/13/2020 8:050
1983/13/2020 8:060
1993/13/2020 13:190
2003/13/2020 13:100
2013/27/2020 11:029
2023/27/2020 11:0215
2044/16/2020 19:526
2054/16/2020 19:496
2065/6/2020 21:136
2075/6/2020 21:126

need to get the output as below

MonthTotalAverage
Jan-2000
Feb-2033
Mar-20930.2
Apr-20126
May-20126
Jun-2000
Jul-2000
Aug-2000
Sep-2000
Oct-2000
Nov-2000
Dec-2000

 

the calculation is for the month Jan 2020. i dont have any records, so the value is zero.

for the month feb 2020, i see two records (2/28/2020 and 2/25/2020 with valuse 3 and 0), here the total is 3+0=3

and the average is 3/1 = 3.

and also same for April, the total is 6+6 =12 and the average is 12/2 = 6.

 

Can anyone help me with this? 

 

Thanks

18 REPLIES 18
sturlaws
Resident Rockstar
Resident Rockstar

Hi @usomaraju 

 

have a look at the attached .pbix-file

 

Cheers,
Sturla

If this post helps, then please consider Accepting it as the solution. Kudos are nice too.

Hi Sturla,

 

Thank you for your quick response, but when i used same formulas i see same result for all months.Capturemonth.PNG

 

the steps i followed, on my table, by using measure i applied the below formula

Total number of days = var _s = SUM('Table'[Days]) return
IF(ISBLANK(_s),0,_s)
 
which is displaying same no of days as in total no of days
Capturetotal.PNG
 
and same for average also.I 'm not sure where i missed.
 
thanks

Did you create a date-table? And a relationship between the date table and your main table?

yes sturlaw, i did.

created the date table and made 1:*relationship between the date table and data table.

but i have one question is, the date column that i shared in my data wast get it from the quick measure column , so i used the original date column for to make relationship,will it cause the issue?

or can i make relationship with quikcmeasure column? if yesy how can we do that?

 

thanks

It need to be a separate table. In the modelling tab, find the 'New table'-button:
image.png

 

and use this piece of dax:

Dates = CALENDAR(DATE(2019,1,1),DATE(2020,4,30))

 

-s

i did same way sturlaw for the date table.

but my doubt is in My data table i had quickmeasure date column where i need to make relationship with datetable date column.

i'm not sure where i did mistake

hi sturla

 

here is my dataset and the date completed is the quickmeasure of other columns in the same table

 

iddaysdate completed
16332/28/2020 7:37
16402/25/2020 12:47
16553/2/2020 15:43
16663/3/2020 4:09
17303/3/2020 12:35
174163/19/2020 19:46
175163/19/2020 19:50
17803/4/2020 14:27
17903/4/2020 14:34
18003/4/2020 14:36
18123/4/2020 15:59
18283/4/2020 15:59
18543/10/2020 14:08
18643/10/2020 14:08
18703/10/2020 14:51
18833/10/2020 14:53
18933/11/2020 16:33
19003/11/2020 16:41
19103/12/2020 8:00
19203/12/2020 8:01
19513/12/2020 16:58
19613/12/2020 17:01
19703/13/2020 8:05
19803/13/2020 8:06
19903/13/2020 13:19
20003/13/2020 13:10
20193/27/2020 11:02
202153/27/2020 11:02
20464/16/2020 19:52
20564/16/2020 19:49
20665/6/2020 21:13
20765/6/2020 21:12

What do you mean by the date completed is the quickmeasure of other columns in the same table

 

Is it a calculated column?

yes sturlaw.

 

in my table i have duplicate ID's with different dates like below, and i applied the quick measure for to get the max date for each id, which is the final result of my date column.

 

Captureduplicates.PNG

 

and with that quick measure column, i'm not able to make relationship with the date table date column, so did 1: * relationship with the original column.

 

can you please help me on getting the monthwise report .

 

Thanks

 

Create a new calculated column in your table like this:

completed date =
VAR _currentID =
    CALCULATE ( SELECTEDVALUE ( 'table'[ID] ) )
RETURN
    CALCULATE ( MAX ( 'table'[date] ), FILTER ( ALL ( 'table' ), id = _currentID ) )

 

and change the measure [total number of days] to 

Total number of days = var _s = calculate(SUM('Table'[Days],filter('table', 'table'[date]='table'[completed date]) return
IF(ISBLANK(_s),0,_s)

 Change the other measure the same way.


If it still does not work, please post a copy of your report

hi sturlaw,

 

Thank you for your quick response

 

here is the result that i get from the formula,if you see, i'm not sure what the values are displaying here for the month of april , may and june.

 

usomaraju_0-1593047360010.png

 

here is my need, i have the below sample data

 

IDDaysDate
1632.92/25/2020 0:00
1632.92/28/2020 0:00
1640.12/25/2020 0:00
1655.23/2/2020 0:00
1665.83/2/2020 0:00
1665.83/3/2020 0:00
17303/3/2020 0:00
17416.33/3/2020 0:00
17416.33/19/2020 0:00
17516.33/3/2020 0:00
17516.33/19/2020 0:00
1780.33/4/2020 0:00
1790.33/4/2020 0:00
1800.33/4/2020 0:00
1812.13/4/2020 0:00
1827.63/4/2020 0:00
1854.43/10/2020 0:00
1864.43/10/2020 0:00
18703/10/2020 0:00
1883.23/10/2020 0:00
1893.23/11/2020 0:00
19003/11/2020 0:00

from here for each id, i need max date and the orresponding days.

 

IDDaysDate
1632.92/28/2020 0:00
1640.12/25/2020 0:00
1665.83/3/2020 0:00
17303/3/2020 0:00
17416.33/19/2020 0:00
17516.33/19/2020 0:00
1780.33/4/2020 0:00
1790.33/4/2020 0:00
1800.33/4/2020 0:00
1812.13/4/2020 0:00
1827.63/4/2020 0:00
1854.43/10/2020 0:00
1864.43/10/2020 0:00
18703/10/2020 0:00
1883.23/10/2020 0:00
1893.23/11/2020 0:00
19003/11/2020 0:00

 

from here i need to total and average per month

 

in my data, for january, i dont see any data, so the value is zero

and for month february, i see 2 results, 2.9+0.1 =3, the total is 3 and the average for the month is 3/2(based on no of id's) = 1.5 and the same  for march sum = 64.2 and the count of id's is 15, so the average value is total/count of the id's

which 64.2/15=4.28

 

so the final report what i need is

 totalaverage
Jan-2000
Feb-2031.5
Mar-2064.24.28
Apr-2000
May-2000
Jun-2000
Jul-2000
Aug-2000
Sep-2000
Oct-2000
Nov-2000
Dec-2000

 

Can you help me please,

 

Thank you

 

 

Have a look at the attached pbix-file

 

-s

hi sturlaw,

 

i'm not able to open the file, get the error mesaage

 

can u please upload it again .

 

Thank you

Anonymous
Not applicable

To open the file just update your PBI Desktop to the latest version.

Best
D

strange. see if works now

still same sturlaw, not able to open the file.

 

i'm not sure why.

 

hi sturlaw,

 

the error i see when i try to open is 'error Object reference not set to an instance of an obj'.

 

and i read so manyy blogs which suggested to installe oracle 64 bit instead of 32 bit, i dont have anything on my PC.

 

can you send me the dax commands, what you used in the file for to get the final result if possible.

 

Thank you in advance

Calculated column in the table containing your data:

IsLastDateFlag =
VAR _id =
    CALCULATE ( SELECTEDVALUE ( 'Table'[ID] ) )
RETURN
    IF (
        'Table'[Date]
            = CALCULATE (
                MAX ( 'Table'[Date] ),
                FILTER ( ALL ( 'Table' ), 'Table'[ID] = _id )
            ),
        1,
        0
    )

 

measure:

Total number of days =
VAR _s =
    CALCULATE (
        SUM ( 'Table'[Days] ),
        FILTER ( 'Table', 'Table'[IsLastDateFlag] = 1 )
    )
RETURN
    IF ( ISBLANK ( _s ), 0, _s )

 

measure:

Average number of days =
VAR _s =
    CALCULATE (
        COUNT ( 'Table'[Date] ),
        FILTER ( 'Table', 'Table'[IsLastDateFlag] = 1 )
    )
RETURN
    DIVIDE ( [Total number of days], _s, 0 )

 

Date table(create new table):

Dates = CALENDAR(DATE(2020,1,1),date(2020,12,31))

 

calculated columns in date table:

Month = FORMAT(Dates[Date],"MMMM")
YearMonth = year(Dates[Date])*100+MONTH(Dates[Date])
MonthNum = month(Dates[Date])

 

Sort Month by MonthNum

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.

Top Solution Authors