I have a calendar table with a YR_Days field which assigns a selling day number for every invoice date. I need help with a DAX expression to calculate total yesterday sales using the YR_Days field. Example data below:
InvoiceDate
YR_DAYS
11/1/2022
200
11/2/2022
201
11/3/2022
202
11/4/2022
202
11/5/2022
202
11/6/2022
203
11/7/2022
204
11/8/2022
205
11/9/2022
206
In the example above, looking at a report for yesterday's sales on 11/6 would show a combined total for 11/3-11/5 since they all have a selling day of 202. However, on 11/7 that report would only show yesterday's sales for 11/6 (203).
Any help would be greatly appreciated!
Use this measure:
Yesterdays Sales =
VAR yesterdays_yr_days =
CALCULATE(
MAX('Table'[YR_DAYS]),
'Table'[InvoiceDate] < MAX('Table'[InvoiceDate])
)
RETURN
CALCULATE(
SUM('Table'[Sales]),
ALL('Table'),
'Table'[YR_DAYS] = yesterdays_yr_days
//,'Table'[InvoiceDate] < MAX('Table'[InvoiceDate])
)
The above formula assumes that you want to have the same sales amount for 11/6, 11/5 and 11/4 since these days all refer to the same selling day number. If you only want to consider past invoice dates, un-comment the penultimate line by removing the //.
Related
I have a table like shown below:
ID
Date
Asset
Location
145
7/29/22
A
Market
145
7/30/22
A
Warehouse
145
7/29/22
B
Market
145
7/29/22
C
Truck
150
7/30/22
B
Market
145
7/29/22
D
Market
145
7/30/22
A
Market
What I am trying to accomplish is to get a distinct count of IDs for each date with a location filter as well. So I would want a count of ID based on the slicer selected Date of 7/29/22 AND 7/30/22 for the Market Location. The desired result is 2 for the selected dates from the date slicer which directly corresponds to the date column in the table.
I was trying to use this DAX formula and wasn't getting anywhere....
IDsMarket =
CALCULATE (
DISTINCTCOUNT ( 'Products'[ID] ),
ALL ( 'Products' )
)
I have a measure dropped onto a card. I should have specified that. My apologies. I need 1 measure to show me the combined count for the two days selected.
I tried this with countrows as well but of course the result wasn't distinct... Any help would be greatly appreciated!!
The formula you're looking for is
IDsMarket =
CALCULATE(
DISTINCTCOUNT('Products'[ID]),
'Products'[Location] = "Market"
)
The resulting Table will look like this
But if you put the measure on a Card visual, you'll get
So in DAX the same measure can yield 1000 different values - depending on the filter context.
I created a conditional column in Power Query and combined the ID with the "day" number from the date column which allowed me to then do a distinct count on that combined custom column which produced to correct answer. Sorry for all the confusion. One of those days.
I have a yearly goal for number of graduates. I want to distribute this yearly number to monthly level using predefined percent numbers.
I would like to get
jan = 1.7% * 292 = 4.96
feb = 1.4% * 292 = 4.01
etc...
The problem is that yearly number of graduates has date of 2021-01-01 and relation to date table, so it will only work for the first month. (Other months are blank). I cannot change the date relation because I have other goals in the same table that use month
Here are my measures
Graduates goal = CALCULATE( SUM(value), Measure = 'Graduates goal')
Goal% = CALCULATE( SUM(value), Measure = 'Graduates%)
Montly graduate target = CALCULATE( [Graduates goal] * [Goal%])
I have tried using ALL(Dates[Month]) ALL(Dates[Year]) but I cannot get past that month level restriction in yearly goal.
Update:
I was able to solve this with crossfilter something like this
Montly graduate target = CALCULATE([Graduates goal], CROSSFILTER( Goals, Dates, None), YEAR(Pvm) = YEAR(TODAY())) * Goal%
I've been struggling with this and hope someone is able to help...
I have a table with sales for products over multiple years. There is a measure that gives me the total revenue for each year by customer, ignoring product sold:
totalRevenueMeasure =
CALCULATE (
SUM ( test[Revenue] ),
ALLEXCEPT ( test, test[company], test[year] )
)
Year company Product revenue totalRevenueMeasure rankx (revenue in year)
2018 company a shoes 100 300 1
2018 company a mugs 200 300 1
2018 Company b shoes 250 250 2
2019 company a lamps 300 300 2
2019 Company b shoes 350 450 1
2019 Company b mugs 100 450 1
2019 Company c mugs 100 100 3
2020 company a shoes 150 150 2
2020 Company c lamps 200 200 1
The closest I got to the RANKX measure is below but this doesn't give the correct results. The expected output is in the RANKX column of the table above.
Customer Rank =
RANKX(
ALLSELECTED( test[company],test[year]),
[TotalRevenueMeasure],
,
DESC,
Dense
)
Thanks in advance for pointers, DAX is still eluding me a bit and there might be a better way to go about it.
Following the recommendation from Alexis, success with test data but live skips some rows in rank - year 2019 doesn't have a rank #1 but has 2 rank #2. I guess this must be some kind of data issue...
You're very close. The problem is that you are looking to rank each year separately but you've removed the Year filter context with your ALLSELECTED function.
Take out the second argument in ALLSELECTED so that you only have company (since you don't actually want to rank over all years for each row).
I have 3 measures:
1) total_trx = SUM(mytable[trx])
2) trx_prev_month = CALCULATE([total_trx], DATEADD(calendar[date], -1,MONTH))
3) monthly_var = DIVIDE([total_trx],[trx_prev_month])-1
If I add a waterfall viz, x-axis with month, it gives me the % of monthly variation and a TOTAL bar at the end that sums all the variations.
I need to reproduce that total number in order to show a KPI as in "so far, we've increased ...%", changing when using a date slicer.
Seems like sum(monthly_var) is not allowed.
Any ideas?
Thank you very much.
Edit1: sample with date filter = Last 4 months
Jul 100 0%
Aug 110 10%
Sep 90 -20%
Oct 80 -10%
Total: -20% <- need a dax to calculate this number and show just -20%
Then if I change the filter to, for example LAST 6 MONTHS, I need to calculate it up to May
In order to get the desired result we will use an intermediate table in our query that will summarize the results by months:
use this code and replace calendar[Year Month] with your Year month column :
SUMX(
SUMMARIZECOLUMNS(calendar[Year Month],"Monthly_int_var",[monthly_var]),
[Monthly_int_var]
)
I have a table of data for two years where I want to retrieve the previous years value for a date. my table the same date shown many times delineated by another value e.g.
Employee, Date, Sales, Prev yr sales
Sam, 1/07/2017, 100
Sam, 2/07/2017, 120
John, 1/07/2017, 90
John, 2/07/2017, 23
etc
Sam, 1/07/2018, 200, 100
Sam, 2/07/2018, 21, 120
John, 1/07/2018, 45, 90
John, 2/07/2018, 130, 23
etc
I am using a dates table created in DAX and have made a relationship between the dates in my table and the dates table.
I have created a measure for the sales & a column for the previous year sales. the latter using calculate and the sameperiodlastyear:
Prev Yr Sales = CALCULATE([Sum Sales],SAMEPERIODLASTYEAR('Calendar'[Date]))
my problem is I cant get the prev yrs sales value out in a table if there are multiple rows per date i.e. sam and john. if there is only one employee the function works.
Can anyone help?
When you are writing a calculated column with a CALCULATE in it, the row context from the table becomes the filter context you are working evaluating in. For this calculation, you don't want any of that row context except for the employee name, so you can remove it using the ALLEXCEPT() function:
Prev Yr Sales =
CALCULATE(
[Sum Sales],
ALLEXCEPT(Sales, Sales[Employee]),
SAMEPERIODLASTYEAR('Calendar'[Date])
)
I've done a quick simulation of the case you've described and found out that the reason you're not getting the expected results might be missing .[Date] on the end of your SAMEPERIODLASTYEAR function argument.
Check out the Data view on my simulation and the highlighted adjustment in the Prev Yr Sales measure:
And the matching result on a Matrix visualization on the Report view:
Hope it helps.