Grouping by two variables (month-year and country) - aggregate

I have to sum the number of deaths by country and by specificdate (i.e. March 2021). See the df
I manage to do it only by country by -->
aggregate(corona$deaths, by = list(corona$country),
FUN = sum)
but I don't know how to specify the exact month and year.

Related

DAX Calculate Billing Days Between Two Variable Dates

I have a dimdate table that is represented below. I have each day flagged as BusinessDay Y/N. I also have a DimSalesRep table that has a daily goal for each rep. I want to be able to allow users to input a StartDt and EndDt with filters on the report and have a calculated column look at the business days between those dates. I can calculate daysbetween with defined dates but I am unsure how I would use DAX with variable dates that are applied through Report filters.
I should also note I am not sure how best to handle a startdt and enddt filter based of the column, TheDate
Cheers!
Reference your dimdate table twice
StartDate = 'dimdate'
EndDate = 'dimdate'
and use this measure:
Num BusinessDays =
CALCULATE(
COUNTROWS('dimdate'),
'dimdate'[BusinessDay] = "Y",
'dimdate'[Date] >= SELECTEDVALUE(StartDate[Date]),
'dimdate'[Date] <= SELECTEDVALUE(EndDate[Date])
)

How to calculate age from different dates and years data

Unfortunately, my dataframe contains a mixture of my 'screenfa' date informations:
ID birthdate
a 01.03.1960
b 1943
c 1987
d 06.12.1985
e 02.06.1984
I would like to calculate the age of the IDs. How can I calculate this? It would be sufficient to calculate the age for all IDs only from the birthyear, but I cannot convert it.
as.POSIXlt(screenfa$birthdate, tz = "",
tryFormats = c('%d.%m.%Y',
'%Y'),
optional = F)
But then R changed for the dates when only the year was known to a wrong/different date.

How to calculate total sales as of first day of the current month as Previous month sales in power BI

I have salesamount column and the saledate column in my table. so I need to calculate total sales for each month based on below calculation.
total sales for current month=sum(salesamount) as of 1st day of the next month
for example sales of December-2021 should be calculated based on the total sales as on Jan-1-2022. total sales for January should be blank until Feb-1-2022 as it should be the total sales as on feb-1-2022. I am very much confused how we can achieve this in Dax. Really appreciate your help.
If I understand your question correctly, you can use the following DAX measure:
Total Sales =
var currentDate = MAX(myTable[saleDate])
var firstOfMonth = DATE(YEAR(CALCULATE(MAX(myTable[saledate]), ALL(myTable))),
MONTH(CALCULATE(MAX(myTable[saledate]), ALL(myTable))), 1)
var result = SUM(myTable[salesamount])
Return IF(currentDate < firstOfMonth, result)
This will take the current date of the report context and compare it to the 1st of the current month. If the currentDate is less than the 1st of the current month, the result will be calculated.
If your dataset has a Date table, you can replace the 'myTable[saledate]' references with a reference to the Date column in your date table.
With some sample data, here are the results:
(I added the firstOfMonth column for demonstration purposes)

Totaling multiple lines into once cell with different date formats

I want to search through a column of dates in the format YYYY-MM-DD (column G - in a random order) and sum up all corresponding cost values for all dates in the same month.
So, for example, the total cost for December 2019 would be 200.
My current formula is:
=SUMPRODUCT((MONTH(G2:G6)=12)*(YEAR(G2:G6)=2019)*(H2:H6))
This gives me the total cost for that month correctly, but I cannot work out how to do this without hardcoding the year and month!
How would I do this with a formula (given the two date columns are a different format)?
You can do this easily combining SUMIFS with EDATE:
SUMIFS function
EDATE function
The formula I've used in cell B2 is:
=SUMIFS($F$2:$F$6;$E$2:$E$6;">="&A2;$E$2:$E$6;"<="&(EDATE(A2;1)-1))
For this formula to work, in column A must be first day of each month!. In cell A2 the value is 01/11/2019, but applied a format of mmmm yyyy to see it like that (and chart will do the same).
paste in D2 cell:
=ARRAYFORMULA(QUERY({EOMONTH(G2:G, -1)+1, H2:H},
"select Col1,sum(Col2)
where Col1 is not null
and not Col1 = date '1900-01-01'
group by Col1
label sum(Col2)''
format Col1 'mmm yyyy'", 0))

Calculate a Reference Date in DAX

Trying to nail down the syntax for a pretty straightforward problem.
I have a table called Events and a full-feature DATES table with a relationship between the Dates[Date] field.
Using the event name as a slicer, I trying to create a [First Monday] measure that will return the date of the first Monday of the month.
So for example, if my event date was 2/14/19, [First Monday] would return 2/4/19.
Your Date table needs to contain 2 columns:
Year-Month: for example, "2018-01"
Weekday Number: for example, 1 (for Monday); or Weekday Name (i.e, "Monday")
Then:
First Monday =
CALCULATE( MIN('Date'[Date]),
ALL('Date'),
VALUES('Date'[Year-Month]),
'Date'[Weekday Name] = "Monday")
How it works:
First, we need to access all dates in the Date table, so we use ALL()
Second, we need to see only dates for the current context year and month, for which we can use VALUES()
Third, for each month we only want Mondays, hence Date[Weekday] = "Monday"
All this unfiltering/filtering generates a set of Mondays for the Year-Month visible in the current filter context. All we need to do now is to find the earliest of the Mondays using Min(Date).