Previous Week Dates using Power Query M in Power Bi - powerbi

I'm using code from Chris Webb:
Source
and here is the sample code from his site:
Ranges = {
{"Today",
TodaysDate,
TodaysDate,
1},
{"Current Week To Date",
Date.From(Date.StartOfWeek(TodaysDate)),
TodaysDate,
2},
{"Current Month To Date",
Date.From(Date.StartOfMonth(TodaysDate)),
TodaysDate,
3},
{"Current Year To Date",
Date.From(Date.StartOfYear(TodaysDate)),
TodaysDate,
4},
.......
I'm looking to get the previous week dates. I tried:
{"Previous Week",
dates.AddWeeks(Date.From(Date.StartOfWeek(TodaysDate,Day.Thursday)),-1),
TodaysDate,
4},
Which works but of course it also adds the dates for the current week (which is Thursday to Thursday in my case). Any ideas about only getting the previous week based on this method? Possibly subtracting the dates from the current week and previous week?
I'm not trying to do week flags or use DAX or R. Preferably in this format.
Anything would help!!! Thanks!

I figured it out.
Here is my final code if anyone is looking for something like this.
It includes Yesterday and Previous Week with a custom week start date. I'm new at this so I guess that's my excuse for it being right in front of me and me missing it!
= {
{"Today",
TodaysDate,
TodaysDate,
1},
{"Yesterday",
Date.AddDays(TodaysDate,-1),
Date.AddDays(TodaysDate,-1),
2},
{"Week to Date",
Date.From(Date.StartOfWeek(TodaysDate,Day.Thursday)),
TodaysDate,
3},
{"Previous Week",
Date.AddWeeks( Date.From(Date.StartOfWeek(TodaysDate,Day.Thursday)),-1),
Date.From(Date.StartOfWeek(TodaysDate,Day.Thursday)),
4},
{"Current Month To Date",
Date.From(Date.StartOfMonth(TodaysDate)),
TodaysDate,
5},
{"Current Year To Date",
Date.From(Date.StartOfYear(TodaysDate)),
TodaysDate,
6},
{"Rolling 13Weeks",
Date.AddWeeks(TodaysDate,-13) + #duration(1,0,0,0),
TodaysDate,
7}
}

Related

Show only one day of the week in Date column

I've created a Date column and used the calendar function to list a date range. Here's the DAX formula I used:
Data = Calendar( Date(2018, 4, 1), Date(2021,4,30))
However, what I'm trying to achieve now that I have the date range specified is I only want Tuesdays to be shown and no other day of the week. How do I achieve this exactly?
Apologies if this is a silly question, I'm completely new to this.
I tried using the Date filters option but that didn't help unfortunately. Thank you!
Simply use a FILTER function:
Data = FILTER(
Calendar( Date(2018, 4, 1), Date(2021,4,30)),
WEEKDAY([Date]) =3)
Bonus for confirmation:
If we want to confirm:
I hope you can accept this answer:
Data =
VAR tbl =
ADDCOLUMNS(
CALENDAR( DATE(2018, 4, 1), DATE(2021, 4, 30)),
"Weekday", WEEKDAY([Date], 2)
)
RETURN
SELECTCOLUMNS(
FILTER( tbl, [Weekday] = 2),
"Date", [Date]
)

Power Bi Dax - Measure that calculates one measure for a period and another if not in that timeframe

I am essentially trying to say from 1/1/2022 through 3/31/2022 use a certain measure and if not in that timeframe then use another measure. The purpose is for these months a different calculation was used to determine the total and for the remainder of the time it was the unchanged, so I want to see the total trended out over time that will not be filtered and for those 3 months show the 'Diff_Total'. I've tried a few things other than this and in this particular attempt it won't let me select a column from the 'Date_Table'...
= CALCULATE(IF(Date_Table > DATE(2022, 01, 01) && Date_Table < DATE(2022, 03, 31), [Diff_Total], [Original_Total]))
I've got it working with
Measure =
VAR Dateinrange =
AND(
min(Date_Table[Date]) > DATE(2022, 01, 01),
min(Date_Table[Date]) < DATE(2022, 03, 01)
)
RETURN IF(Dateinrange=TRUE(),[Diff_Total], [Original_Total])
Let me know if that works for you!

How to sort a field named 'Day of Week' as Monday, Tuesday, Wednesday, Thursday, Friday?

This is my first time using MS Power BI. I am trying to figure out how to sort a days of the week that come from Excel as Monday, Friday, Wednesday, etc. I tried to add a Weekday function, and apparently that needs a Date, but all I have is Monday, Friday, Wednesday, etc. My source is an Excel Pivot Table. Do I need to go to the source of the Pivot, or is there a way to do Monday=1, Tuesday=2, Wednesday=3, and then sort descending?
Now, if I click 'Day of Week' and click the 3 dots (ellipsis), I can sort ascending/descending, but neither options sets it to this: Monday, Tuesday, Wednesday, Thursday, Friday. This is the sort order that I want to create.

trying to create a date table in Redshift

I'm trying to create a date table in redshift with the following columns:
date
year
month
month name
year month
month name year
note that year month is in the format : '202001' and month name year is in the format: january2020
. how do I insert data in the table after I create it?
appreciate the help..
This is fairly straight forward in Redshift. A recursive CTE can be use to make the list of dates and some simple formatting can be used to get the fields you desire. This example makes all the dates from Jan 1, 2020 until today.
with recursive dates(dt) as
( select '2020-01-01'::date as dt
union all
select dt + 1
from dates d
where d.dt <= current_date
)
select dt, extract(year from dt) as year, extract(month from dt) as month,
decode(month, 1, 'January', 2, 'February', 3, 'March', 4, 'April', 5, 'May', 6, 'June', 7, 'July', 8, 'August', 9, 'September', 10, 'October', 11, 'November', 12, 'December') as mname,
year * 100 + month as yearmo, mname || year::text as monameyr
from dates order by dt;
Frankly, the easiest way to create a Calendar table is to make it in Excel!
Make a row for each date, the use formulas to add columns for day, month, year, day number, etc. You can also add booleans to identify weekends and public holidays. You could add a column for the first and last days of the month (eg to lookup the last day of a month after a given date).
Then, save the data as a CSV file and import it into the Redshift table.
Yes, you could write some fancy SQL using the generate_series() function that makes a table purely within Redshift, but it can be a little painful to write and doing it in Excel gives you a chance to add other functionality (such as public holidays).

How to dynamic filter on Power BI?

How can I make a dynamic filter on a Power BI chart?
I have a MONTH table and it contains these values: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. I do not want to display data for months that are bigger than the current month, MONTH(TODAY()).
I want something like this:
You can set up a measure instead to determine whether to show the particular month or not as follows:
Show = IF(FIRSTNONBLANK('Month'[Month], 13) <= MONTH(TODAY()), 1, 0)
And then you can add this measure to Visual level filters and set it equal to 1.
Results: