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

Help with calculating a sum of a measure by weeknum

I have been messing around with a report on Covid 19 cases based on ideas created by another user on the internet. From the Johns Hopkins data it shows the total confirmed and total deaths for each day. I use a measure to determine the daily new cases. I would like to also create a measure to calculate the new cases by the weeknumber. I have a calendar table. 

My data looks like this:

 

 

DateCombined_Key Total ConfirmedWeek NumberMeasure(Daily New Cases)
3/4/2020New York City, New York, US110 
3/5/2020New York City, New York, US4103
3/6/2020New York City, New York, US11107
3/7/2020New York City, New York, US11100
3/8/2020New York City, New York, US12101
3/9/2020New York City, New York, US19117
3/10/2020New York City, New York, US25116
3/11/2020New York City, New York, US551130
3/12/2020New York City, New York, US951140
3/13/2020New York City, New York, US1541159
3/14/2020New York City, New York, US26911115
3/15/2020New York City, New York, US269110


The answer I am looking for is a total cases in week 10 of 12, and a total cases in week 11 of 257. To be clear the last column is a measure. The first three columns is what the data looks like from Johns Hopkins University. 

I'm not sure if I should be doing this in the query or in DAX in the report. 

Thanks for your assistance. 

1 ACCEPTED SOLUTION

Hi @william_johnson 

 

I prefer Power Query. Take a look at the solution in the appendix.

 

 

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast


View solution in original post

14 REPLIES 14

Hi @william_johnson ,

 

was your problem solved?

 

 

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast


No it wasn't. 

Hi @william_johnson 

 

I prefer Power Query. Take a look at the solution in the appendix.

 

 

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast


Slaps head... geez it's so easy once you see it.

Still looking for ideas...

Hi, @william_johnson 

Can you share part of the data(the Johns Hopkins data ) corresponding to the original table?

It will be difficult for us to  conduct  further analysis.

 

Best Regards,
Community Support Team _ Eason

 

 

I'll do you one better, here is the data source: https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_ti...

I use a measure to get to the new cases by creating the total confirmed measure, and then subtracting yesterday's total confirmed from today's (basically). I want to somehow (as I noted, be able use total the daily new cases by weeknumber.

 

 

Not sure how to apply this to my question. I think my issue is that I am calculating the daily cases as a measure, not as a column. I am concerned that the weeknum and the such require a column. 

Hi, @william_johnson 

Calculated column will be a better choice.

Please try dax as below:

C_weeknum = WEEKNUM('Table_C'[Date]-1)
new cases added = 
var previous_confirmed= CALCULATE(SUM('Table_C'[Total Confirmed]),PREVIOUSDAY('Calendar'[Date]))
return IF(previous_confirmed=BLANK(),BLANK(),'Table_C'[Total Confirmed]-previous_confirmed)

 

Please check the sample file .

Pbix attached 

 

Best Regards,
Community Support Team _ Eason

 

So my Total Confirmed is a meausre as well. When I try to turn it into a column and use the nca column I get the dreaded circular dependency issue.

In the table there is a confirmed, and to get the total confirmed by the combined_key field which is the County and State a measure was created:

Total Confirmed =
VAR sum_confirmed=
Sumx(
Values('Covid 19 US Data'[Province_State]),
SUMX(
Values('Covid 19 US Data'[Combined_Key]),
Calculate(Max('Covid 19 US Data'[Confirmed]))
)
)
Return
if(sum_confirmed=0,Blank(), sum_confirmed)

I'm thinking I need to add in that language to the NCA formula as well, but I also need the total confirmed... I'm a newbie. Thanks for your assistance.

Hi , @william_johnson 

Can you share part of the data corresponding to the original table?
Is "Total Confirmed" grouped by  fields [Province_State] and field [Combined_Key]  to sum up [Confirmed]?

If yes , you can  try calculate column as below:

Total Confirmed column =
CALCULATE (
    SUM ( 'Covid 19 US Data'[Confirmed] ),
    'Covid 19 US Data',
    'Covid 19 US Data'[Combined_Key] = EARLIER ( 'Covid 19 US Data'[Combined_Key] ),
    'Covid 19 US Data'[Province_State]
        = EARLIER ( 'Covid 19 US Data'[Province_State] )
)

 

Best Regards,
Community Support Team _ Eason
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-easonf-msft Thanks for the assistance let me try and get a couple lines of data: The Your Total Confirmed is the results I get from your code.

DateProvince_StateCombined_KeyConfirmedTotal ConfirmedDaily New CasesYour Total Confirmed
3/2/2020 0:00New YorkNew York City, New York, US11 20918754
3/3/2020 0:00New YorkNew York City, New York, US11020918754
3/4/2020 0:00New YorkNew York City, New York, US11020918754
3/5/2020 0:00New YorkNew York City, New York, US44320918754
3/6/2020 0:00New YorkNew York City, New York, US1111720918754
3/7/2020 0:00New YorkNew York City, New York, US1111020918754
3/8/2020 0:00New YorkNew York City, New York, US1212120918754
3/9/2020 0:00New YorkNew York City, New York, US1919720918754
3/10/2020 0:00New YorkNew York City, New York, US2525620918754
3/11/2020 0:00New YorkNew York City, New York, US55553020918754
3/12/2020 0:00New YorkNew York City, New York, US95954020918754


Daily New Cases Measure:  

Daily New Cases =
VAR current_day = [Total Confirmed]
VAR prev_day =
CALCULATE (
[Total Confirmed],
DATEADD ( 'Calendar'[Date], -1, DAY )
)
RETURN
IF ( ISBLANK ( prev_day ), BLANK(), current_day - prev_day )
 
Total Confirmed is also a measure created to make the calculations easier I believe. 
Total Confirmed =
VAR sum_confirmed=
Sumx(
Values('Covid 19 US Data'[Province_State]),
SUMX(
Values('Covid 19 US Data'[Combined_Key]),
Calculate(Max('Covid 19 US Data'[Confirmed]))
)
)
Return
if(sum_confirmed=0,Blank(), sum_confirmed)

Everything works but the ability to sum up the current weeks totals... it just may not be possible, but I'm hoping it is. 

Any thoughts out there? I still haven't figured it out.

 

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.