Overlapping dates in PowerQuery - date

So I have a table with StartDate and EndDate and I need to know if this dates overlap in a natural year. In example January 1st 2021 and December 31st 2021 and know how many days where in that period.
I have a formula that works in PowerBI that is:
Note: "DATA" is the name of the query.
OVERLAP days 2021 = MAX(MIN(DATE(2021,12,31),'DATA'[StartDate].[Date])-MAX(DATE(2021,1,1),'DATA'[EndDate].[Date])+1,0)
But now I need to translate it to PowerQuery.
Thanks!

How about
let Source = #table({"StartDate", "EndDate"},{{#date(2021,5,31),#date(2021,7,31)}}),
year=2021,
#"Added Custom" = Table.AddColumn(Source, "Overlap", each
if [EndDate]<#date(year,1,1) or [StartDate]>#date(year,12,31) or [EndDate]<[StartDate] then null else
Number.From(if [EndDate]<#date(year,1,1) then #date(year,1,1)
else if [EndDate]>#date(year,12,31) then #date(year,12,31)
else [EndDate]) -
Number.From(if [StartDate]<#date(year,1,1) then #date(year,1,1)
else if [StartDate]>#date(year,12,31) then #date(year,12,31)
else [StartDate]))
in #"Added Custom"

The basic algorithm is pretty much the same. But I confess I got a bit carried away with implementing it.
The problem is in how you want to define the years of interest, and how you want to display the result when there are multiple years.
To use Power Query
Select some cell in your Data Table
Data => Get&Transform => from Table/Range or from within sheet
When the PQ Editor opens: Home => Advanced Editor
Make note of the Table Name in Line 2
Paste the M Code below in place of what you see
Change the Table name in line 2 back to what was generated originally.
Read the comments and explore the Applied Steps to understand the algorithm
Here is a method using various M-Coding to develop
a list of all the years that are represented in the table
List.Generate to generate a list of days in any one Year
List.Accumulate to iterate through List.Generate for each year
Some other functions to create the table with the additional columns.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}}),
//add columns for each year
//create list of years for the table
Years =
let
firstYear = List.Transform(#"Changed Type"[Start Date], each Date.Year(_)),
lastYear = List.Transform(#"Changed Type"[End Date], each Date.Year(_))
in
List.Numbers(List.Min(firstYear), List.Max(lastYear) - List.Min(firstYear)+1),
//create lists of days count for each year, based on the start and end dates
newcols = List.Accumulate(Years, {}, (state, current) => state &
{let
ends = List.Transform(#"Changed Type"[End Date], each List.Min({#date(current,12,31), _})),
starts = List.Transform(#"Changed Type"[Start Date], each List.Max({#date(current-1,12,31), _})),
dys = List.Generate(
()=>[d=List.Max({0,Duration.Days(ends{0} - starts {0})}), idx=0],
each [idx] < List.Count(ends),
each [d=List.Max({0,Duration.Days(ends{[idx]+1} - starts {[idx]+1})}), idx=[idx]+1],
each [d]
)
in
dys}),
//add the new columns to original table
newColNames = List.Transform(Years, each "Days in " & Text.From(_)),
newTable = Table.FromColumns(
Table.ToColumns(#"Changed Type") & newcols,
Table.ColumnNames(#"Changed Type") & newColNames),
//Set data types
typed = Table.TransformColumnTypes(newTable, List.Transform(newColNames, each {_, Int64.Type}))
in
typed

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])
)

Unable to display view using two Parameters in Tableau

I have a "Select Year" Parameter for Year.
If I select from "Select Year", then I want to display results for Selected Year and Previous Year (both) in a CrossTab View on the same Sheet.
Please refer to attached to know more about my query.
*Other way (unwanted): *
I tried to create two Calculated Fields (Dimensions Filters for selecting the Year), then created two different sheets - "Selected Period Value" and "previous Period Value", but I want just one sheet to display it together.
//Selected Year
IF YEAR([Year]) = INT([Year Parameter]) THEN
INT([Year Parameter])
END
//Previous Year
IF YEAR([Year]) = INT([Year Parameter])-1 THEN
INT([Year Parameter])-1
END
Please assist.
Just assuming 46.34% and 46.85% as Profit measure.
Create two calculated fields, one for the year selected from parameter and the other for prior year.
Profit_Current calculated field:
IF YEAR([Year]) = INT([Year Parameter]) THEN Profit
ELSE 0 END
Profit_Previous calculated field:
IF YEAR([Year]) = INT([Year Parameter])-1 THEN Profit
ELSE 0 END
Use these two calculated fields in cross tab
Let us know if this works!

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).

Qlikview Automate past 12 months selection

I have a combo chart which shows the average days it took for a person to pay their bill.
The Dimension of the Chart is = [Pay Month Year last 12 months]
There are no Dimension limits
There is 1 expression which is called Average and its definition is:
avg({< InvoicefromSqlType = {'Invoices'},[Is Invoice Paid] = {'Y'},[Is Positive Amount] = {'Y'},[Is Paid last 12 months] = {'Y'},DueGroups=,[Pay Month Year last 12 months]=>}[Days to Pay])`
Its is sorted by expression, which is [Pay Month Year last 12 months]
Now the above fields are built up like this:
[Pay Month Year last 12 months]
If([Pay Date] >= '$(vPeriodS12)',[Pay Month year]) as [Pay Month Year last 12 months],
PayLoadOrder:
Load * Inline [Pay Month Year last 12 months
May-2014
Jun-2014
Jul-2014
Aug-2014
Sep-2014
Oct-2014
Nov-2014
Dec-2014
Jan-2015
Feb-2015
Mar-2015
Apr-2015
May-2015
];
Now what is happening is every month when it reaches the end, the next month is needing to be manually added and the first month removed (e.g. in the above I would delete the line May-2014 and at the end add the line Jun-2015)
Also if there are months defined which there is no data for yet i.e. you have Jun-2015 hard coded and the current month this May-2015 then Jun-2015 will show data from 2014 and the order of the months will get mixed up.
What I want to do is completely remove the need to hard code the months above and have this done its self.
If there are any more information you need let me know
Instead of using a "sort order" table that you may have to manually update, it may be worth doing the following:
Create a new field derived from [Pay Date] that returns a month and year that you can sort. For example:
dual(date(makedate(year([Pay Date]),num(month([Pay Date]))),'MMM-yyyy'),
year([Pay Date]) * 100 + num(month([Pay Date]))) as PayMonthYear
Here, the dual function allows you to associate a different representation of a field value to its underlying value. For example, here we set the underlying data to be the year of [Pay Date] added to the month, but state that it should be displayed as MMM-yyyy. For example, internally, QV still sees the value 201502, but displays it as Feb-2015. This means you can sort it correctly based on its underlying value.
Using dual is a big topic, please consult the built-in help for QV for more information.
Change your chart dimension from [Pay Month Year last 12 months] to use PayMonthYear and set the sort to ascending. This would then mean that your months would be sorted correctly, even if a new month was added.
Remove table PayLoadOrder from your script.
Alternative Method
An alternative would be to use a calendar table which joins on your Pay Date field. This would achieve the same thing, however, you could also integrate your "year to date" indicator into the calendar as well and remove it from your main table. An example I quickly threw together is shown below:
MinMax:
LOAD
Max([Pay Date]) AS MaxDate,
Min([Pay Date]) AS MinDate
RESIDENT MyData;
LET varMinDate = Num(Peek('MinDate',0,'MinMax')); // 0 is first record
LET varMaxDate = Num(Peek('MaxDate',-1,'MinMax')); // -1 is last record
LET varToday = Num(Today());
MasterCalendar:
LOAD
monthstart([Pay Date]) >= monthstart(AddMonths(Today(),-12)) as PaidInLast12MonthsFlag,
dual(date(makedate(year([Pay Date]),num(month([Pay Date]))),'MMM-yyyy'),year([Pay Date]) * 100 + num(month([Pay Date]))) as PayMonthYear
[Pay Date];
LOAD
date($(varMinDate) + RecNo() - 1,'DD/MM/YYYY') as [Pay Date]
AUTOGENERATE num($(varMaxDate)) - num($(varMinDate)) + 1;
DROP TABLE MinMax;
So, in the above, the field PaidInLast12MonthsFlag is equal to -1 if the value of the [Pay Date] field occurs in the last 12 months, 0 otherwise. You could use this in your set analysis expression as a filter. Furthermore, you can use PayMonthYear as your chart dimension.

Date query with the current date between two date_time columns

I have a fusion table with two date_time columns. The fist one is the start date (Startdatum) and in the other column is the end date (Einddatum).
I want to do a query with the current date, and only show the KML-lines on a map where the current date lies between the start and end date.
I tried to use the code below to create a string with a date format:
var time_date = new Date();
var day = time_date.getDate();
var month = time_date.getMonth()+1;
var year = time_date.getFullYear();
var date = (year+"."+month+"."+day);
To show the KML-lines on the map I tried to use the following code:
layer = new google.maps.FusionTablesLayer({
map: map,
heatmap: { enabled: false },
query: {
select: "col2",
from: "1mOMP1seJq4FdiNTugsfylZaJc8sKcSlfJKUuTJjv",
where: "'Startdatum' <= date AND 'Einddatum' >= date"
},
options: {
styleId: 2,
templateId: 2
}
});
Unfortunatly the map shows all the KMS-lines regardless what date is in one of the columns.
What am I doing wrong?
the where-clause is wrong, it has to be
where: "Startdatum <= '"+date+"' AND Einddatum >= '"+date+"'"
the date-format seems to be wrong. Although the used format yyyy.MM.dd is defined in the documentation, it doesn't work. The format yyyy-MM-dd currently works for me(but it's not defined in the documentation).
var date = (year+"-"+month+"-"+day);
(in case that day and month be less than 10 they wouldn't match the pattern, but that doesn't seem to be an issue)
Beyond that: when you fix these 2 mentioned parts it currently works(for me), but I've tried it a couple of hours ago and got unstable results.