How to filter dates in calculate column - powerbi-desktop

I have a report in Power BI where there is 'Date' Column.
I want to create a calculated column named 'Invoicing Month' based on the 'Date' column.
But the issue is the invoicing month starts from 21st of each month and ends on 20th of next month and for December, the invoicing month starts from 16th December and ends on January 20th.
Hence the invoicing months are:
16th Dec. - 20th Jan.
21st Jan. - 20th Feb.
21st Feb. - 20th Mar.
...
...
21st Nov. - 15th Dec.
I have used following statement for the calculated column but getting error for the date.
**Invoice Month =
if(TimesheetView[Date].[Date]< 16-12-2023, "December 2023",
if(TimesheetView[Date].[Date] <= 20-11-2023,"November 2023",
if(TimesheetView[Date].[Date] <=20-10-2023, "October 2023",
IF(TimesheetView[Date].[Date] <=20-09-2023, "September 2023",
IF(TimesheetView[Date].[Date] <=20-08-2023, "September 2023",
IF(TimesheetView[Date].[Date] <= 20-08-2023, "August 2023",
IF(TimesheetView[Date].[Date] <= 20-07-2023, "July 2023",
IF(TimesheetView[Date].[Date] <= 20-06-2023, "June 2023",
IF(TimesheetView[Date].[Date] <= 20-05-2023, "May 2023",
IF(TimesheetView[Date].[Date] <= 20-04-2023, "April 2023",
IF(TimesheetView[Date].[Date] <= 20-03-2023, "March 2023",
IF(TimesheetView[Date].[Date] <= 20-02-2023, "February 2023",
IF(TimesheetView[Date].[Date] <= 20-01-2023, "January 2023",
IF(TimesheetView[Date] < 16-12-2022, "Old Data",
"NA"
))))))))))))))**
But "NA" is appearing in all results?

Related

Excel Formula caculating number days of year between 2 dates

I am looking for an excel formula to calculating the number of days from years between "Start date" & "end date"
Example :
My start date is 06/19/2020
My end date is 06/20/2023
I would to know on this example the number of days from year 2020 => from start date ; year 2021 (full year) ; year 2022 (full year) ; 2023 => from end date
Result :
number of days of 2020 : 195
number of days of 2021 : 365
number of days of 2022 : 365
number of days of 2023 : 170
in my databse i have different start date and end date by line to x year
Thanks for your help
You can simply substract one cell value from another, if for example:
A1 = TODAY() (which returns today's date)
A2 = 1/01/2019
B1 = A1-A2 = 765 days
Hope it was helpful for you
You might be interested in the DATEDIF function:
https://stackoverflow.com/a/73268311/13406526
Sorry, but I'm a newbie, cannot add comments.

Nested IF statement to read and output custom date groupings

I am trying to create a nested IF statement in Excel 2016 to read a date value and output the corresponding financial month. Since a financial month is unique to the company I work for there is no available alternative other than to create a custom function or statement.
So far I have tried:
=IF(D2 <= DATEVALUE("28/07/2017"),DATEVALUE("Jul-2017"),
IF(D2 <= DATEVALUE("26/08/2017"),DATEVALUE("Aug-2017"),
IF(D2 <= DATEVALUE("29/09/2017"), DATEVALUE("Sep-2017"),
IF(D2 <= DATEVALUE("27/10/2017"), DATEVALUE("Oct-2017"), 0)))) +
IF(D2 <= DATEVALUE("24/11/2017"), DATEVALUE("Nov-2017"),
IF(D2 <= DATEVALUE("29/12/2017"), DATEVALUE("Dec-2017"),
IF(D2 <= DATEVALUE("26/01/2018"), DATEVALUE("Jan-2018"),
IF(D2 <= DATEVALUE("23/02/2018"), DATEVALUE("Feb-2018"), 0)))) +
IF(D2 <= DATEVALUE("20/03/2018"), DATEVALUE("Mar-2018"),
IF(D2 <= DATEVALUE("27/04/2018"), DATEVALUE("Apr-2018"),
IF(D2 <= DATEVALUE("25/05/2018"), DATEVALUE("May-2018"),
IF(D2 <= DATEVALUE("29/06/2018"), DATEVALUE("Jun-2018"),
IF(D2 <= DATEVALUE("27/07/2018"), DATEVALUE("Jul-2018"),
IF(D2 <= DATEVALUE("24/08/2018"), DATEVALUE("Aug-2018"), 0)))))) +
IF(D2 <= DATEVALUE("28/09/2018"), DATEVALUE("Sep-2018"),
IF(D2 <= DATEVALUE("26/10/2018"), DATEVALUE("Oct-2018"),
IF(D2 <= DATEVALUE("23/11/2018"), DATEVALUE("Nov-2018"),
IF(D2 <= DATEVALUE("28/12/2018"), DATEVALUE("Dec-2018"),
IF(D2 <= DATEVALUE("25/01/2019"), DATEVALUE("Jan-2019"),
IF(D2 <= DATEVALUE("22/02/2019"), DATEVALUE("Feb-2019"),
IF(D2 <= DATEVALUE("29/03/2019"), DATEVALUE("Mar-2019"), 0))))))) +
IF(D2 <= DATEVALUE("26/04/2019"), DATEVALUE("Apr-2019"),
IF(D2 <= DATEVALUE("24/05/2019"), DATEVALUE("May-2019"),
IF(D2 <= DATEVALUE("28/06/2019"), DATEVALUE("Jun-2019"), D2)))
It isn't working - I get a numeric output instead of a date.
There must be a better/effective way of doing this.
Can somebody recommend another approach? Possible via UDF in VBA?
Edit - posting a table of the ranges and required output
Start date End date Month
12/30/2017 1/26/2018 Jan-18
1/27/2018 2/23/2018 Feb-18
2/24/2018 3/30/2018 Mar-18
3/31/2018 4/27/2018 Apr-18
4/28/2018 5/25/2018 May-18
5/26/2018 6/29/2018 Jun-18
6/30/2018 7/27/2018 Jul-18
7/28/2018 8/24/2018 Aug-18
8/25/2018 9/28/2018 Sep-18
9/29/2018 10/26/2018 Oct-18
10/27/2018 11/23/2018 Nov-18
11/24/2018 12/28/2018 Dec-18
12/29/2018 1/25/2019 Jan-19
1/26/2019 2/22/2019 Feb-19
2/23/2019 3/29/2019 Mar-19
3/30/2019 4/26/2019 Apr-19
4/27/2019 5/24/2019 May-19
5/25/2019 6/28/2019 Jun-19
The pattern in your table is:
The last Friday in each month is the month cut off, which means....
The last Saturday in each month is the start of the next month
This will take a input, verify that it is a date, and see if the date is before, equal to, or after the last Friday in the input month.
If before, CSTMonth = Inputs Month
If equal, CSTMonth = Inputs Month
If after, CSTMonth = Month after Inputs Month
Option Explicit
Public Function CSTMonth(Target As Range)
If IsDate(Target) Then
Dim LastDay As Date: LastDay = WorksheetFunction.EoMonth(Target, 0)
Dim LastFri As Date: LastFri = LastDay - (Weekday(LastDay + 1) Mod 7)
If Target <= LastFri Then
CSTMonth = Format(Target, "MMM-YY")
Else
CSTMonth = Format(LastDay + 1, "MMM-YY")
End If
End If
End Function
The benefit of finding the pattern in your table is that this will never need to be updated with brute force. Taking your approach would mean hard coding each and every months start and end dates.
If you are using a table with the starting dates for each period the try the following:
Assuming the table with the period starting dates is located at B2:D25 and the dates are located at F2:F25 and J2:J25
Enter this formula to obtain the Financial Period as date serial in G2 then copy it to G3:G25 and K2:K25:
=VLOOKUP(F3,$B$2:$D$25,3,1)
Enter this formula to obtain the Financial Period as text in H2 then copy it to H3:H25 and L2:L25:
=TEXT(VLOOKUP(F3,$B$2:$D$25,3,1),"mmm-yyyy")
I am not sure about the complications you are facing but to get the desired results per your table, you can also use a simpler formula like
=TEXT(EOMONTH(A2,1),"YY-MMM")
OR
=EOMONTH(A2,1)
and format the cell in yy-mmm format.
where A2 holds the start date.

Chaining Multiple IF Statements in Excel to Calculate Work Shifts from Times

I am trying to calculate a work shift based on the time entered in an Excel worksheet. The shift allocation is as below:
If Time >= 05:30 PM & <= 01:30 AM - 1st shift
If Time >= 01:30 AM & <= 09:30 AM - 2nd shift
If Time >= 09:30 AM & <= 01:30 AM - 3rd shift
I am using IF AND conditions but still it's showing incorrect results. Below is the formula I am using.
=IF(AND(D2>=TIMEVALUE("05:30:00 PM"),(D2<=TIMEVALUE("01:30:00 AM"))),"1",
IF(AND(D2>=TIMEVALUE("01:30:00 AM"),(D2<=TIMEVALUE("09:30:00 AM"))),"2","3"))
Any idea what I am doing wrong?
The first problem is in your shift definitions. The time 1:30 AM falls in all three shifts due to the use of less-than-or-equal-to operators. These should be changed to less-than operators, such as the following:
If Time >= 5:30 PM & < 1:30 AM - 1st shift
If Time >= 1:30 AM & < 9:30 AM - 2nd shift
If Time >= 9:30 AM & < 5:30 PM - 3rd shift (note: I corrected an apparent typo here as well)
The second problem is in the first if statement:
IF(AND(D2>=TIMEVALUE("05:30:00 PM"),(D2<=TIMEVALUE("01:30:00 AM"))),"1",...)
The time can't simultaneously be both greater than 5:30:00 PM and less than 01:30:00 AM. Times in Excel are stored as numbers. TIMEVALUE("1:30 AM") returns 0.06250 and TIMEVALUE("5:30 PM") returns approximately 0.72917. As a result this if statement translates to the following:
IF(AND(D2>=0.72917, D2<=0.06250), "1", ...)
D2 cannot be both greater-than-or-equal-to 0.72917 and less-than-or-equal-to 0.06250. As a result this will always return false.
Try rewriting your code as follows:
=IF(D2<TIMEVALUE("1:30 AM"),"1",
IF(D2<TIMEVALUE("9:30 AM"),"2",
IF(D2<TIMEVALUE("5:30 PM"),"3","1")))

Find the end-of-month date from any date entered in MS Excel

I need to take the date from one cell, find the end of the month that that date falls into, then add one calendar month.
Example:
Enter jan 13: jan 13 end of month is Jan 31, + 1 month is Feb 28 (or 9 on leap year).
Feb 19: = March 31
March 2 = 30 April
March 30 = 30 April
March 31 = 30 April
etc etc
Is there an excel formula that will do this? I don't want any macro/ VBA stuff.
If you have 2007 or later than:
=EOMONTH("3/30/2016",1)
Use the Date function, add one to the month and set the Day to zero. This gives the last day of the month. To get the last day of the next month add two to the month.
=DATE(YEAR(E3),MONTH(E3)+1,0)
=DATE(YEAR(E3),MONTH(E3)+2,0)

Convert the date from PST to IST and output the difference in hour

Is there any way to automatically convert PST Date in to IST date. When I put date in column E and F I have dates in PST time zone. I want to convert in IST time zone. after i want output in column G. the difference between two dates.
I have solved this with excel formula but I want this to done with VBA.
For eg.
> E F G
1/14/2016 2:49:00 PM 1/14/2016 2:57:00 PM 0:08
1/14/2016 2:45:00 PM 1/14/2016 2:59:00 PM 0:09
Consider:
Sub qwerty()
Dim pst As Date, ist As Date
pst = DateSerial(2016, 1, 14) + TimeSerial(14, 49, 0)
ist = pst + TimeSerial(13, 30, 0)
MsgBox pst & vbCrLf & ist
End Sub
String string = "December 16, 2016 12:10:05 PST";
DateFormat format = new SimpleDateFormat("MMMM dd, yyyy HH:mm:ss z", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
Output : Sat Dec 17 01:40:05 IST 2016

Resources