Calculate YTD , MTD, WTD, Transaction Count in Power BI - powerbi

I have 1 table with multiple rows. It looks something like this:
------------------------------------------------
StoreId| PostingDate | SalesAmt
MAIN | 2021-02-04 | 100
WEST | 2021-08-11 | 15
WEST | 2021-09-11 | 36
MAIN | 2021-11-11 | 78
MAIN | 2021-04-11 | 56
------------------------------------------------
And soon and so forth...
Now I want to produce the following in the Power BI as Table:
--------------------------------------------
StoreId| YTD | MTD | WTD | TransactionCount |
WEST |5,447| 800 | 74 | 1,475 |
MAIN |4,500| 421 | 15 | 1,855 |
--------------------------------------------
How can I achieve that? I am very new to this so I don't know how to do it.
I have been reading DAX and Power Query but maybe DAX is suitable for this?

I assumed your data looks like this. I've added data for the year 2022.
Also, I'm assuming there aren't future dates, the observations behave as transactions that happened in the past.
Table
StoreID
PostingDate
SalesAmt
WEST
16/01/2021
141
MAIN
24/01/2021
221
WEST
25/01/2021
119
MAIN
18/04/2021
209
MAIN
22/04/2021
220
MAIN
24/04/2021
167
WEST
16/11/2021
224
WEST
03/02/2022
155
MAIN
07/02/2022
236
WEST
11/02/2022
216
WEST
23/03/2022
135
MAIN
28/05/2022
153
WEST
01/06/2022
121
Calendar Table
For the calculations below to work, you need to create a calendar table.
It goes from the first date of Table until today.
If your calendar table is different the time intelligence function will not work.
Calendar = CALENDAR(MIN('Table'[PostingDate]),TODAY())
And mark the Calendar table as a Date Table.
Sales Amount
Sales Amount = sum('Table'[SalesAmt])
WTD
Assumes your week starts on Monday.
WTD =
VAR WeekStart = TODAY() - WEEKDAY(today(),2)
RETURN
CALCULATE([Sales Amount],'Table'[PostingDate]>=WeekStart)
MTD:
MTD =
TOTALMTD([Sales Amount],'Calendar'[Date])
YTD
YTD =
TOTALYTD([Sales Amount],'Calendar'[Date])

Related

How to find YTD daily average in Power Bi?

I am trying to figure out how to calculate a "New Measure" in my power BI visual that calculates the YTD daily average.
So for example,
my query on the backend would look like this
App Date | ID | Subject
01\01\2022 | 123 | Math
01\01\2022 | 456 | Science
01\02\2022 | 789 | Science
01\02\2022 | 012 | History
01\03\2022 | 345 | Science
01\03\2022 | 678 | History
01\03\2022 | 921 | Art
01\03\2022 | 223 | Science
01\04\2022 | 256 | English
Im trying to calculate what the daily average is YTD for math, science, history etc.
I tried
1 Daily average = calculate (average(Query[app date]))
I know this is not correct but I would appreciate any help..
Measure= AVERAGEX(VALUES(Date[APP_DATE]), [Subject])
This forumula should work to count YTD Average
First you need to create a date table using New Table and paste this code:
Date =
VAR ModelDate =
ADDCOLUMNS(
CALENDAR(DATE(2022,01,01), DATE(2022,12,31)),
"Year", YEAR([Date]),
"Month",FORMAT([Date],"mmm"),
"MonthNumber", MONTH([Date]))
RETURN
ModelDate
Then Create a relationship between date table and your fact table:
Then Go on to write this code AS Your measure:
Daily Avg_YTD =
CALCULATE(
AVERAGEX(
VALUES(YourTbl[Subject]),COUNT(YourTbl[Subject])),
DATESYTD('Date'[Date]))
Now If you put it on a table visual:
I hope It solves your problem!

Adding a measure which finds the next row value for every row (similar to SQL Lead window function)

will be very grateful if you could share your experience and advice on the following problem in Power BI:
3 Tables given in the data model:
calendar dimension table
fact table on sessions
fact table on spending
| CW | Total cost | Sessions | Expected Column 1 | Expected Column 2 |
+----+-------------+-----------+-------------------+-------------------+
| 1 | 1200 | 50 | | |
| 2 | 1500 | 60 | 1200 | 50 |
| 3 | 1700 | 48 | 1500 | 60 |
| 4 | 1150 | 36 | 1700 | 48 |
| 5 | 900 | 29 | 1150 | 36 |
+----+-------------+-----------+-------------------+-------------------+
CW column indicates the calendar week and it is from calendar table. Sessions and Total cost are from sessions and spending tables respectively. Data is aggregated and visualized on calendar week level.
Problem: I need to create measures to derive Expected column 1 and expected column 2 based on total cost and sessions columns. Basically getting next values for each row similar to lead window function.
I have checked power BI community and there are several ideas (for example here https://community.powerbi.com/t5/Desktop/DAX-Query-to-Find-Next-Value/td-p/833896).
But these solution assume all columns are from the same table, however in the above described case
all 3 columns are from different tables.
Will the be possible to get expected columns 1 and 2 and how? Many thanks in advance!

Use of count command

I am using a dataset, which among other variables includes the following:
. describe year country co brand
storage display value
variable name type format label variable label
----------------------------------------------------------------------------------------------------------------
year int %9.0g year (=first dimension of panel)
country byte %9.0g market market (=second dimension of panel)
co int %9.0g model code (=third dimension of panel)
brand byte %21.0g brand brand code
After I load the dataset, I generate a new variable and declare my data to be panel:
egen yearcountry = group(year country), label
xtset co yearcountry
I would like to estimate the market share of each brand in each country.
For example:
count if brand=="AlfaRomeo" & country=="Italy"
However, i get the following error:
type mismatch
r(109);
The entire dataset consisting of 11,483 observations can be downloaded from here.
The following works for me:
. count if brand == 1 & country == 4
111
The variables brand and country are not string but numeric with value labels:
. tabulate country
market |
(=second |
dimension |
of panel) | Freq. Percent Cum.
------------+-----------------------------------
Belgium | 2,641 23.00 23.00
France | 2,252 19.61 42.61
Germany | 2,281 19.86 62.47
Italy | 2,020 17.59 80.07
UK | 2,289 19.93 100.00
------------+-----------------------------------
Total | 11,483 100.00
. taulate country, nolabel
market |
(=second |
dimension |
of panel) | Freq. Percent Cum.
------------+-----------------------------------
1 | 2,641 23.00 23.00
2 | 2,252 19.61 42.61
3 | 2,281 19.86 62.47
4 | 2,020 17.59 80.07
5 | 2,289 19.93 100.00
------------+-----------------------------------
Total | 11,483 100.00
However, note that what you are calculating here is not the market share, but the number of cars of a particular brand in a certain country. The percentage market share is usually defined as the ratio of unit sales and total market unit sales.
The following code snippet will thus produce what you want:
forvalues i = 1 / 47 {
bysort year (country): egen a_`i' = total(brand == `i')
bysort year (country): gen b_`i' = (a_`i' / _N) * 100
}
collapse b_*, by(country year)
You can also check that the results add up as follows:
egen all = rowtotal(b_*)
You could then see the market share for AlfaRomeo & Audi, for years 1970 & 1976 and for Belgium & France as follows:
format b_* all %4.2f
list year country b_1 b_2 all if inlist(year,1970, 1976) & inlist(country, 1, 2), noobs
+---------------------------------------+
| year country b_1 b_2 all |
|---------------------------------------|
| 1970 Belgium 3.31 7.35 100.00 |
| 1976 Belgium 5.01 4.13 100.00 |
| 1970 France 3.31 7.35 100.00 |
| 1976 France 5.01 4.13 100.00 |
+---------------------------------------+

Calculating average NETWORK days Power BI

I've been reading various threads and guides to performing a 'NETWORKDAYS' style calculation in Power BI but struggling to make it work.
I have a table like this:
Team | Meeting | Report
aaa | 1/1/2018 | 9/1/2018
aaa | 1/1/2018 | 7/1/2018
bbb | 1/1/2018 | 1/2/2018
bbb | 1/1/2018 |
ccc | 1/1/2018 | 3/3/2018
aaa | 1/1/2018 |
And I want to return the average days without weekends and holidays, something like this:
Team | average
aaa | 5 (10/2)
bbb | 23 (45/1)
ccc | 45 (45/1)
I have this function, which seems to work albeit clunkily, but I don't know how to remove the non-weekdays from the Date table:
AVERAGEX(FILTER(Planning,NOT(ISBLANK(Planning Actual_FinalReport]))),
(COUNTROWS(DATESBETWEEN(DateTable[Date],
Planning[Actual_ClosingMeeting],Planning [Actual_FinalReport]))
))
Where DateTable is:
Date | Weekday
5/1/2018| 1
6/1/2018| 0
7/1/2018| 0
and so on...
Essentially, I want to iterate over Planning (filtering out blanks in [Report]) and count the dates between Meeting and Report from the Dates table, filtered by Weekday = 1. It's the syntax to link the tables I think I'm struggling with.
Modifying your formula a bit, how about something like this?
Average = AVERAGEX(
FILTER(Planning,
NOT(ISBLANK(Planning[Actual_FinalReport]))),
COUNTROWS(
FILTER(DateTable,
DateTable[Date] IN DATESBETWEEN(
DateTable[Date],
Planning[Actual_ClosingMeeting],
Planning[Actual_FinalReport]) &&
DateTable[Weekday] = 1)))
I'm adding the Weekday filter at the end.

Stata table: how to compute difference column without adding a new variable?

In a panel data set, I'm using
table Region TIME if TIME==2014 | TIME==2020 | TIME==2030 | TIME==2040, contents(sum BF ) row
to create the following table:
------------------------------------------
| TIME
Region | 2014 2020 2030 2040
----------+-------------------------------
701 | 26751 27941 29944 31477
702 | 10456 11354 12723 13788
704 | 41550 44481 49340 53273
706 | 44976 47535 51940 55573
709 | 43258 44398 46612 48191
711 | 6580 7011 7539 7856
713 | 9036 10139 11776 13194
714 | 3091 3284 3563 3750
716 | 9144 9730 10724 11543
719 | 5719 6292 7258 8036
720 | 11509 12161 13188 13919
722 | 21403 22344 23839 25006
723 | 4927 5094 5345 5447
728 | 2460 2576 2761 2906
|
Total | 240860 254340 276552 293959
------------------------------------------
I'd like to add a fifth column, which displays the difference between the year 2014 and 2040 in %.
Question: is this possible WITHOUT adding a new variable to the dataset? For instance by letting the fifth column being derived from a formula?
If not, how do I easily compute a new variable, taking account of the long format of the panel data set?
This isn't possible within table.
Your variable could be something like
egen total2014 = total(BF / (TIME == 2014)), by(Region)
egen total2040 = total(BF / (TIME == 2040)), by(Region)
gen pcdiff = 100 * (total2040 - total2014)/total2014
after which you can tabulate its (mean) value for each region. See Section 10 in http://www.stata-journal.com/sjpdf.html?articlenum=dm0055 for the first trick here.
You may need to go outside table for the tabulation, but if all else fails, collapse to a new dataset of totals and means.