First occurence logic - power bi - dax

I have a table with data like below
Event name| Event Id| Acct Date | Event Date
Event1 1 2022-01-01 2022-01-01
Event1 2 2022-04-01 2022-01-01
Event1 3 2022-01-01 2022-01-01
Event1 4 2022-09-01 2022-01-01
Event2 1 2022-01-01 2022-01-01
Event2 2 2022-04-01 2022-01-01
Event2 3 2022-01-01 2022-01-01
Event2 4 2022-09-01 2022-01-01
Question:
I want to print a column saying Event Count with values 1 and 0
First Occurence,
If Acct Date = Event Date, then Event Count should be 1
Second Occurence,
If Acct Date = Event Date, then Event Count should be 0
because we already have a match for the same event name and Acct Date = Event Date combination.
How do i achieve the above in power bi dax? Im new and any help would be greatly appreciated. tx
if(Acct Date = Event Date,1,0), this gives me event count as 1,0,1,0 , But what i need is 1,0,0,0. This should happen for every event name and Acct Date = Event Date combination.

Add an extra column with the following DAX:
First Occurence =
var EventName = 'Table'[Event name]
var EventId = 'Table'[Event Id]
var occurCount = CALCULATE(COUNTROWS('Table'), FILTER('Table', 'Table'[Acct date] = 'Table'[Event Date] && EventName = 'Table'[Event name] && EventId > 'Table'[Event Id]))
return IF(ISBLANK(occurCount), 1, 0)
We run over the rows and for each row we are going to filter the table based on criteria. The criteria are based on Event name, Event Id and if the Acc Date = Event Date.
If you want to see what occurCount is ging you you cna peek by:
First Occurence =
var EventName = 'Table'[Event name]
var EventId = 'Table'[Event Id]
var occurCount = CALCULATE(COUNTROWS('Table'), FILTER('Table', 'Table'[Acct date] = 'Table'[Event Date] && EventName = 'Table'[Event name] && EventId > 'Table'[Event Id]))
return occurCount

On whether your condition is met 1 is returned, else 0. The idea of the SWITCH is to embed multiple conditions which is a powerful to incorporate while working:
Event Count = SWITCH(TRUE(), COUNTROWS (TableName) > 1, 1, 0)

Related

How can I update dates in a cursor in FoxPro?

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

How to get DateTime difference in Dax?

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

What is the best way to work around data?

I am trying to display data as such:
In our database we have events (with unique ID) and then a start date. The events do not overlap, and each one starts on the date the last one ended. However we don't have 'end date' in the database.
I have to feed the data into another system so that it shows event ID, start date, and end date (which is just the next start date).
I want to avoid creating a custom view as that's really frowned upon here for this database. So I'm wondering if there's a good way to do this in a query.
Essentially it would be:
EventA | Date1 | Date2
EventB | Date2 | Date3
EventC | Date3 | Date4
The events are planned years in advance and I only need to have the next few months pulled for the query, so no worry about running out of 'next event start dates' and in case it matters, this query will be part of a webservice call.
The basic pseudo code for event and date would be:
select Event.ID, Event.StartDate
from Event
where Event.StartDate > sysdate and Event.StartDate < sysdate+90
Essentially I want to take the next row's Event.StartDate and make it the current row's Event.EndDate
Use the LEAD analytic function:
Oracle Setup:
A table with 10 rows:
CREATE TABLE Event ( ID, StartDate ) AS
SELECT LEVEL, TRUNC( SYSDATE ) + LEVEL
FROM DUAL
CONNECT BY LEVEL <= 10;
Query:
select ID,
StartDate,
LEAD( StartDate ) OVER ( ORDER BY StartDate ) AS EndDate
from Event
where StartDate > sysdate and StartDate < sysdate+90
Output:
ID | STARTDATE | ENDDATE
-: | :-------- | :--------
1 | 22-JUN-19 | 23-JUN-19
2 | 23-JUN-19 | 24-JUN-19
3 | 24-JUN-19 | 25-JUN-19
4 | 25-JUN-19 | 26-JUN-19
5 | 26-JUN-19 | 27-JUN-19
6 | 27-JUN-19 | 28-JUN-19
7 | 28-JUN-19 | 29-JUN-19
8 | 29-JUN-19 | 30-JUN-19
9 | 30-JUN-19 | 01-JUL-19
10 | 01-JUL-19 | null
db<>fiddle here

Oracle Apex: Update selective columns based on column range

I have columns as 001,002,003 till 254. want to update columns based on start date and end date range. Let's say I have selected start date as 01 Jan and end date as 10 Jan, then table columns 001-010 should get updated with user id, rest columns will be as it is.
Probably with multiple cases
UPDATE YourTable
SET 001 = CASE WHEN 2017-01-01 BETWEEN startRange AND endRange
THEN newValue
ELSE 001
END,
002 = CASE WHEN 2017-01-02 BETWEEN startRange AND endRange
THEN newValue
ELSE 002
END,
....
xxx = CASE WHEN 2017-0x-0x BETWEEN startRange AND endRange
THEN newValue
ELSE xxx
END,

Doctrine 2 - How to search with DQL and grouping

I can't figure out how i can combine my LIKE search with grouping it by date and a category in DQL..
Explanation:
I have a search function that search my events after weight.. it looks like this:
SELECT e as event,
(CASE
(e.name LIKE :searchterm) THEN 10
ELSE 0
END) +
(CASE
WHEN (e.name LIKE :searchtermFull) THEN 5
ELSE 0
END)
AS weight
FROM
\Entities\Event e
WHERE
e.name LIKE :searchterm OR
e.name LIKE :searchtermFull
ORDER BY weight DESC
This works fine, but now i want to have these searchresults grouped by year and date (not time) so my items is grouped like this:
27 jan:
event 1
event 2
28 jan:
event 3
event 4
And i also need to do is to get it grouped my categories so i in the end will have:
27 jan:
Category 1
Event 1
Event 2
Category 2
Event 3
My Database event table:
name varchar(255)
description varchar(255)
created datetime
place_id int(11)
eventdate datetime
organiser_id int(11)
created_user_id int(11)
category int(11)
My Database category table
id int(11)
name varchar(255)
description varchar(255)
I have a Event Entity and a EventCategory Entity and what the EventCategory does is just to bind the category id on to the Event Entity
--Update - my sql so far--
SELECT
c.name,
YEAR(e.eventdate) as year,
MONTH(e.eventdate) as month,
DAY(e.eventdate) as day,
count(*) as total
FROM `events` as e, `eventCategories` as c
WHERE
e.category = c.id
GROUP BY
YEAR(e.eventdate),
MONTH(e.eventdate),
DAY(e.eventdate)
ORDER BY eventdate ASC
The DoctrineExtensions is included by writing this in the autoloader:
$classLoader = new Doctrine\Common\ClassLoader('DoctrineExtensions', ROOT.DS."library");
$classLoader->register();
You don't need to write your own function or install third party library. Since datetime is stored as string, you can use Doctrine's function SUBSTRING() to extract certain part and group by it.
For example:
SELECT SUBSTRING(e.eventdate, 1, 4) AS year FROM … GROUP BY year;
You need to use date functions DAY, MONTH or YEAR (Mysql)
Doctrine as is does not support these functions so you need to write your own (DQL functions) or install this library
Group records by year, month
WHERE ... GROUP BY YEAR(eventdate), MONTH(eventdate) ORDER BY eventdate ASC
Group records by year, month and day
WHERE ... GROUP BY YEAR(eventdate), MONTH(eventdate), DAY(eventdate) ORDER BY eventdate ASC

Resources