Friday, March 31, 2023
Storms in Pursuit
Wednesday, March 29, 2023
Sequencing Months and Years in Power BI
Sequencing months within Power BI can be a bit awkward, if you need to constantly shift months to show a rolling 12 or however many, while continually keeping them in the order in which they fall across years.
Solutions of the complex variety abound. Simple answers are rare, likely because most of the folks writing them have been mired in code for entirely too long. That is not to say they are wrong, just that there appears a tendency towards over-programming.
A simple solution only requires a little bit of math to produce results perfectly adapted to ordering months and their associated years in visualizations.
It can be done with or without a calendar dataset; either way, a date field is required.
In Power Query, add a [Year] and [Month] columns, both of the numeric variety.
The next step is to simply add a Custom Column with the following, which is M language, indicating to take the date and convert it to text and concatenate the two.
= Text.Combine({Date.ToText([#”Date”], “yyyy”), “ “, Date.ToText([#”Date”], “MMM”)})
The output in the new column is “2023 Mar” for March 2023.
Lastly, create one more Custom Column with the following code (M again).
=[Year]*100+[Month]
The output in the new column is “202303” for March 2023.
Mission accomplished, almost.
Back in Power BI, sort one column by the other and all months from here into eternity are sorted correctly by year and month.
Compare that with a DAX solution captured from somewhere a few months ago, hailed in some parts of the interweb as the greatest thing since sliced bread. It works just as well, relying on the same columns, but for non-programmer types is not quite as easy to wrap your head around, much less remember it over and over again.
Seq Month =
VAR MaxMos = SUMMARIZE(ALL(‘Calendar’),’A Calendar’[Year],”MaxMonth”,MAX(‘Calendar’[Month]))
VAR MyYear = [Year]
VAR MyStart = SUMX(FILTER(MaxMos,[Year]<MyYear),[MaxMonth])
VAR firstYear = CALCULATE(FIRSTNONBLANK(‘Calendar’[Year],1),ALL(‘Calendar’))
VAR myNum = IF(MyYear=firstYear,[Month],MyStart+[Month])
RETURN myNum
There are likely limitations with the simple approach, though none have been uncovered as of this writing. Power BI can slice and dice in a hundred different ways and seems to care very little whether the code is complex or simple math.
I like simple.
Tuesday, March 28, 2023
Get the latest file from Power BI
Daily or weekly reports constantly need data appended to a source folder in non-database situations. As the files pile up folder it can be a little difficult to differentiate between them in Power BI, when one really only needs the latest for a particular visualization or report page.
Another story on Medium, linked below, does a great job of detailing how to get only the latest file data. The solution points to creating a separate, identical query on the same pile of data and then, singling out the most recent report in the new query.
A few standard conventions are required to do this. Naming files to include a YYYYMMDD date prefix makes it a little easier to perform the sort needed but inserts an extra “hands on” step in the process. [Date Modified] or [Date Created] from the file attributes usually prove more useful.
While all of this is effective enough, it can make things a little clunky; especially true if seeking to have the queries always mirror one another. There are quite a few elegant solutions to do just that too, if one digs around enough.
There is a simpler solution. It still requires sorting the data in Power Query. It does not require another query.
In Power BI, a slicer leveraging whatever field used to do that sort pulls off the same stunt. Place a “Top N” filter on that slicer to show only the “Top 1” based on whatever seems most appropriate. In my use case, it is a date field and works perfect every time, with every “Scheduled Refresh,” and appears a little more flexible too.
There are a few limitations on editing the text that appears in the slicer. A few modifications may be needed to get the right look. However, it enables the report to host as many visualizations as needed, based on one data file, without an extra query and the need to maintain that query. I suspect it probably performs better too.
. . .
further reading
Popular Variations
-
A rainbow stretched across the horizon, commending our efforts of the day, though the level of exertion really did not warrant such a fanta...
-
If it takes this much to train folks how to use AI, then you can probably drop the "I." There is no intelligence, it is all jus...
-
Enough fooling around, let's get down to making a basic Power BI Report. You will need a properly formatted Excel file of your data. If ...
-
Only just now made aware of the use of the above photograph on a click-bait site, I was a bit annoyed. It was not so much that the photo wa...