I have a SQL query which calculated the difference between the current weekday and previous weekdays in terms of weeks and I need to translate this to DAX query.
SQL :
DATEDIFF(Week,Getdate(),
DATEADD (WEEK, CAST (RIGHT(CAST ([Code] AS nvarchar),2) AS int),
DATEADD (YEAR, ([Code] / 100)-1900, 0)) - 4 -
DATEPART(dw,
DATEADD (WEEK, CAST (RIGHT(CAST ([Code] AS nvarchar),2) AS int),
DATEADD (YEAR, ([Code] / 100)-1900, 0)) - 4) + 1)
AS [WeekIndex]
enter code here
Expected Result: If code = 201821 then WeekIndex has to be -53 as the current week is week 22
I think you are looking for something like this:
[Measure] :=
VAR code = "201821"
VAR code_year = LEFT ( code, 4 )
VAR code_week = RIGHT ( code, 2 )
VAR code_date = DATE ( code_year, 1, -2 ) - WEEKDAY ( DATE ( code_year, 1, 3 ) )
+ code_week * 7
RETURN
DATEDIFF ( TODAY (), code_date, WEEK )
VAR [code_date] will return the monday of the given VAR [code] as a datevalue
Related
I am trying to compare a date defined by the user with a date I have in a table and count 7 days at a time until the dates match. Basically, it’s counting weeks, but how can I get it update the date in the cursor?
Set Date BRITISH
users_year = Year(users_date) && users_date is a public variable in week1get.scx
Select * From "t:wiptrack\routecard\week1" Where;
year = users_year Into Cursor ben
Select ben
Goto Top
my_date = ben.firstmon
*count up to week
X = 0
Do While users_date >= my_date
my_date.DATE() + 7 && Trying to add 7 days to date
X = X + 1
If X >= 100
Messagebox("Count is greater than 100")
Endif
Loop
Enddo
You have to store the new value somewhere after you compute it. In addition, the DATE() function doesn't do what you think. Assuming the my_date variable contains a date, you do this:
my_date = my_date + 7
I have table which has an a DateTime column, status column and an index column, I am trying to get a column which shows the time difference between the previous row and the current row and for it to ignore the previous row status column is blank.
This is what the table looks like, currently:
Time ID status. timesinceprev(seconds) index
22.1.21 04:02:04 12 low 0 1
22.1.21 04:24:07 12 low 1320 2
22.1.21 04:26:04 12 medium 120 3
22.1.21 04:29:04 12 180 4
22.1.21 04:30:05 12 61 5
I want to change the timeSinceprev to show the time difference in the format HH:MM:SS when the previous row status column is blank, Here is what I have currently with the query:
timeCol =
var tempcol=
MINX(FILTER('Table',
'Table'[ID]=EARLIER('Table'[ID])
),'Table'[Time])
var filtertemp =
EARLIER('Table'[status])
RETURN IF(filtertemp<>BLANK(),FORMAT('Table'[Time]-tempcol,"HH:MM:SS"))
Use EARLIER like following in combination with ID and Index to get the previous row value when status=blank, grouped by ID
Column =
VAR _1 =
SWITCH (
TRUE (),
'Table'[status] = BLANK (),
MAXX (
FILTER (
'Table',
'Table'[ID] = EARLIER ( 'Table'[ID] )
&& 'Table'[Index] < EARLIER ( 'Table'[Index] )
),
'Table'[Time]
)
)
VAR _2 =
SWITCH (
TRUE (),
'Table'[status] = BLANK (), CALCULATE ( MAX ( 'Table'[Time] ) )
)
VAR _3 =
FORMAT ( _2 - _1, "HH:MM:SS" )
RETURN
_3
How to adjust the below solution source
to show census year (CY) which starts 16th of April and ends 15th of April?
(i.e.: for dates between '2020-04-16' AND '2021-04-15': 'CY2020-21', for dates between '2019-04-16' AND '2020-04-15': 'CY2019-20, and so on).
Financial Year =
VAR fy =
IF (
MONTH ( 'Dates'[Dates] ) <= 3,
VALUE ( FORMAT ( 'Dates'[Dates], "YY" ) ) - 1,
VALUE ( FORMAT ( 'Dates'[Dates], "YY" ) )
)
RETURN
CONCATENATE ( "FY", CONCATENATE ( fy, CONCATENATE ( "/", fy + 1 ) ) )
You can do this a bit more cleanly as follows:
CY =
VAR yr = FORMAT ( STARTOFYEAR ( 'Dates'[Date], "4/15" ), "YY" )
RETURN
"CY" & yr & "-" & yr + 1
Note: For this to work properly for the earliest year, the Dates table should start at the beginning of the census year.
I have a Power BI visual as below. There are 3 matrices. I have a DateDimension (or) Calendar table called Dates2.
I use two measures, one a regular measure (called 'Count'), the other a parallel period comparison of the measure (called 'Count_PreviousYear'). I use SAMEPERIODLASTYEAR DAX function for the latter.
1)
Count = COUNTA(TableX[ColumnY])
--Measure with name 'Count'--
2)
Count_PreviousYear = CALCULATE
(
[Count],
SAMEPERIODLASTYEAR(Dates2[Date])
)
--Measure with name 'Count_PreviousYear'
--this measure uses Time Intelligence function - SAMEPERIODLASTYEAR--
Both 'Count' and 'Count_PreviousYear' (obviously) are not YTD (YearToDate) values.
A third measure for the percentage change across periods is computed as below:
3)
PercentageChange = IF(
ISBLANK([Count]) || ISBLANK([Count_PreviousYear]),
BLANK(),
(([Count] - [Count_PreviousYear])/[Count])
)
Kindly ignore the fact that a keyword used as a measure name;
I have used the name 'Count' only for clarity; in my actual report,
I have proper names
The % change measure works fine, but one issue:
For the period change from 2020 to 2021, i.e. in the third row of the last matrix (for the row value 2021), the total (i.e. the % change value) is not appropriate.
I need to replace -737.21% with - 23.98 %.
This is because , I need to compute the Total for 2020, only by adding the values for the months of January and February, i.e. 428 + 430 = 858. (not 5794, which is for all the 12 months).
Since 2021 has only two months - January and February, I don't want to compare two months of 2021, with all the 12 months of 2020. Rather, I want two months of 2021 to be compared with the corresponding 2 months of 2020.
Essentially I need {(692-858)/692} * 100 = -23.98%
Currently, I see {(692-5794)/692} * 100 = -737.21%
Can someone help me achieve this?
Count Previous Year =
IF (
HASONEVALUE ( Dates2[Month] ),
IF (
[Count] <> BLANK (),
CALCULATE ( [Count], SAMEPERIODLASTYEAR ( Dates2[Date] ) )
),
IF (
HASONEVALUE ( Dates2[Year] ),
CALCULATE (
[Count],
DATESBETWEEN (
Dates2[Date],
EDATE ( MIN ( Dates2[Date] ), -12 ),
EOMONTH ( MAX ( [FactTable[Date] ), -12 )
)
)
)
)
Count_PreviousYear = IF (
(HASONEVALUE(Dates2[Year]) = TRUE && HASONEVALUE(Dates2[MonthName]) = TRUE),
CALCULATE
(
[Count],
SAMEPERIODLASTYEAR(Dates2[Date])
),
IF (
(HASONEVALUE(Dates2[Year]) = TRUE && HASONEVALUE(Dates2[MonthName]) = FALSE),
CALCULATE (
[Count],
DATESBETWEEN (
Dates2[Date],
EDATE (MIN(Dates2[Date]), -12),
EOMONTH (MAX(SourceData[Date]), -12)
)
),
BLANK()
)
)
!Output obtained as desired]1
I'm having date as 41293 in oracle, how can i show it in DD/MON/YYYY format?
If i copy pasted it in Excel and change it to date format, it shows 01/19/13
Please help me.
The value you have is the number of days since the 30th of December 1899. Try:
select to_char(
to_date('1899-12-30', 'YYYY-MM-DD') + 41293,
'DD/MON/YYYY') from dual
Quoting from Oracle forum:
You need a tool to do that, since format is to tell oracle what type of format you have on your date type in the spreadsheet. While you may not have opted to format the date in Excel, it will appear as a date in the previewer. Use the format from this as a guide to enter into the datatype panel.
so, if you have a date that looks like this in the previewer, 19-jan-2006, then your format for the data type panel if you choose to insert that column is going to be DD-MON-YYYY,
Option 1:
Try using the below functions
FUNCTION FROMEXCELDATETIME ( ACELLVALUE IN VARCHAR2 )
RETURN TIMESTAMP
IS
EXCEL_BASE_DATE_TIME CONSTANT TIMESTAMP
:= TO_TIMESTAMP ( '12/31/1899',
'mm/dd/yyyy' ) ;
VAL CONSTANT NUMBER
:= TO_NUMBER ( NULLIF ( TRIM ( ACELLVALUE ),
'0' ) ) ;
BEGIN
RETURN EXCEL_BASE_DATE_TIME
+ NUMTODSINTERVAL ( VAL
- CASE
WHEN VAL >= 60
THEN
1
ELSE
0
END,
'DAY' );
END;
FUNCTION TOEXCELDATETIME ( ATIMESTAMP IN TIMESTAMP )
RETURN VARCHAR2
IS
EXCEL_BASE_DATE_TIME CONSTANT TIMESTAMP
:= TO_TIMESTAMP ( '12/31/1899',
'mm/dd/yyyy' ) ;
DIF CONSTANT INTERVAL DAY ( 9 ) TO SECOND ( 9 )
:= ATIMESTAMP
- EXCEL_BASE_DATE_TIME ;
DAYS CONSTANT INTEGER := EXTRACT ( DAY FROM DIF );
BEGIN
RETURN CASE
WHEN DIF IS NULL
THEN
''
ELSE
TO_CHAR ( DAYS
+ CASE
WHEN DAYS >= 60
THEN
1
ELSE
0
END
+ ROUND ( ( EXTRACT ( HOUR FROM DIF )
+ ( EXTRACT ( MINUTE FROM DIF )
+ EXTRACT ( SECOND FROM DIF )
/ 60 )
/ 60 )
/ 24,
4 ) )
END;
END;
Option 2:
The excel function would be =TEXT(B2,"MM/DD/YY"), to convert an Excel date value stored in B2. Then try using the test character in Oracle
If considering 1900 Jan 1st as start date,
SELECT
TO_CHAR ( TO_DATE ( '1900-01-01',
'YYYY-MM-DD' )
+ 41293,
'DD/MON/YYYY' )
FROM
DUAL
Microsoft's Documentation
Excel stores dates as sequential serial numbers so that they can be used in calculations. January 1, 1900 is serial number 1, and January 1, 2008 is serial number 39448 because it is 39,447 days after January 1, 1900.
Excel has a bug feature where it considers 1900 to be a leap year and day 60 is 1900-02-29 but that day never existed and a correction needs to be applied for this erroneous day.
It does also state that:
Microsoft Excel correctly handles all other leap years, including century years that are not leap years (for example, 2100). Only the year 1900 is incorrectly handled.
Therefore only a single correction is required.
So:
Before 1900-03-01 you can use DATE '1899-12-31' + value.
On or after 1900-03-01 you can use DATE '1899-12-30' + value.
Which can be put into a CASE statement:
SELECT CASE
WHEN value >= 1 AND value < 60
THEN DATE '1899-12-31' + value
WHEN value >= 60 AND value < 61
THEN NULL
WHEN value >= 61
THEN DATE '1899-12-30' + value
END AS converted_date
FROM your_table