How to compare values by different dates in PowerBI - powerbi

My problem is related with PowerBI report
It is example table, Real table contains 10000+ results
user
salary
date
1
123
14-10-2022
2
455
11-10-2022
3
333
13-10-2022
4
222
12-10-2022
5
111
10-10-2022
desired output:
user
salary
date
salary (date-1 day)
salary (date-3 days)
1
123
14-10-2022
333
455
2
455
11-10-2022
111
3
333
13-10-2022
222
111
4
222
12-10-2022
455
5
111
10-10-2022
How can I achieve it in PBI ?
I tried to merge tables but the dashboard was very slow after try like that.

I would do this in DAX, not Power Query.
Add a date dimension (search on this if you aren’t familiar with date dimensions), then create these DAX measures.
Salary (date-1 day) = CALCULATE( SUM(TABLE[salary]), DATEADD(DateDim[DateKey],-1,day) )
Salary (date-3 day) = CALCULATE( SUM(TABLE[salary]), DATEADD(DateDim[DateKey],-3,day) )

Related

How to select data from a table based on multiple expressions?

I have a view by the name of info and it's structure and data sample is the following:
id
name
contacts
1
ali
1234
1
ali
122
2
john
133
2
john
144
2
john
122
3
mike
111
4
khan
444
5
jan
122
5
jan
155
So I am using the above view data in oracle apex report. I want to search data by id for example I search for id=1, it contains two values in contacts column one of the value which is 122 is also included in another records so the result should also contain all the other records which contain 122 in their contacts column.
The expected result which I want is:
id
name
contacts
1
ali
1234
1
ali
122
2
john
133
2
john
144
2
john
122
5
jan
122
5
jan
155
We can phrase your requirement as wanting to return any record with id = 1 or any record whose contacts overlap with the contacts of id = 1.
SELECT id, name, contacts
FROM yourTable
WHERE id = 1 OR
id IN (
SELECT id
FROM yourTable
WHERE contacts IN (SELECT contacts FROM yourTable WHERE id = 1)
)
ORDER BY id;
Demo

DAX formula providing the previous value of a variable, sorted by a different variable

I have two tables, Table1 and Table2, containing the following information:
Table1: Sales
Date Firm A Firm B Firm C
30-05-2022 100 200 300
29-05-2022
28-05-2022
27-05-2022 130 230 330
26-05-2022 140 240 340
25-05-2022 150 250 350
and
Table2: Dates
Relative day Date
1 30-05-2022
2 27-05-2022
3 26-05-2022
4 25-05-2022
In my Power BI (PBI) desktop a slicer, allowing the user to select from a range of Relative days (i.e. number of business days from today's date), is present.
What I want is to create a new measure, Sales lag, that contains the lagged value of sales for the individual firm, and for which the lagged value is based on the Relative day variable e.g.:
For the slicer set according to Relative day=1
Sales Sales lag
Firm A 100 130
Firm B 200 230
Firm C 300 330
Please note that I (think I) need the measure to be based on the relative day variable, as the Date variable does not take into account business days.
I previously used a measure that I think was similar to:
Sales lag =calculate(sum(Table1[Sales],dateadd('Table2'[Date],-1,day))
While this measure provided the correct results most of the time, it did not in the presence of weekends.
Thank you
I used some sample data structured slightly differently than you provide and took a stab at providing you with a solution. Find the sample data I used below and the Sales lag measure in the Solution Measure section.
Table1
Date
Firm
Sales
30-05-2022
A
100
29-05-2022
A
110
28-05-2022
A
120
27-05-2022
A
130
26-05-2022
A
140
25-05-2022
A
150
30-05-2022
B
200
29-05-2022
B
210
28-05-2022
B
220
27-05-2022
B
230
26-05-2022
B
240
25-05-2022
B
250
30-05-2022
C
300
29-05-2022
C
310
28-05-2022
C
320
27-05-2022
C
330
26-05-2022
C
340
25-05-2022
C
350
Table2
Relative day
Date
1
30-05-2022
2
27-05-2022
3
26-05-2022
4
25-05-2022
Solution Measure
Sales lag =
VAR sel = SELECTEDVALUE(Table2[Relative day])
VAR dat = CALCULATE(MAX(Table2[Date]), Table2[Relative day] = sel + 1)
RETURN
IF(
ISBLANK(sel),
"",
CALCULATE(
MAX(Table1[Sales]),
ALL(Table2[Relative day]),
Table1[Date] = dat
)
)
Sample Result

Flag everytime when ID change date DAX

I have table where with orders, articles belonging to orders and their shipping dates. What I want to do is, flag every time when shipping date changed or (when all dates for OrderID are the same) flag only once.
I tried to use calculated columns wrote in DAX, like nextdate, prevdate, nextorder, prevorder and reffer to them, but it doesn't work
I would appreciate every tip how to solve my prblem. Thanks!
OrderID
Article ID
Shipping date
Flag
123
1
01.01.2012
1
123
2
01.01.2012
0
123
1
02.01.2012
1
1234
12
15.03.2012
1
678
12
25.05.2014
1
678
345
25.05.2014
0
678
567
25.05.2014
0

DAX query to filter Invoices where at least one line item contains a particular product

I have an invoice table with columns including InvoiceNo and ProductID. I need to be able to filter so that it returns all Invoices that contain a particular Product. When this filter is applied, it needs to only display the invoices with that product, but it should show all products on that invoice.
For example, I have the following table:
InvoiceNo ProductID
111 ProdA
111 ProdB
111 ProdC
222 ProdA
222 ProdB
I only want to display invoices that have ProdC. When filtered, it needs to show
InvoiceNo ProductID
111 ProdA
111 ProdB
111 ProdC
I have tried the following DAX Measure:
CALCULATE(
COUNTROWS(Invoice),
SUMMARIZE(
FILTER(
SUMMARIZE(
Invoice
,Invoice[InvoiceNo]
,Invoice[ProductID]
)
,Invoice[ProductID] = "ProductC"
)
,Invoice[InvoiceNo]
)
)
But when I apply the filter (where measure > 0), it only returns the following
InvoiceNo ProductID
111 ProdC
If I was doing this in SQL, I would use the following query:
select
[InvoiceNo]
,[ProductID]
from
[Invoice]
where
[InvoiceNo] in (SELECT [InvoiceNo] FROM [Invoice] where [ProductID] = 'ProdC')
UPDATED: 19th July
A further complication is that we also have a Qty Column, which can contain the value 0. So an updated table would be
InvoiceNo ProductID Qty
111 ProdA 1
111 ProdB 2
111 ProdC 1
222 ProdA 1
222 ProdB 3
333 ProdA 1
333 ProdB 2
333 ProdC 0
I want to exclude Invoice 333 from my result, because while I am looking for invoices that contain ProdC, I only want them if ProdC qty is > 0.
This measure will do the trick
Active =
SUMX (
Products ;
CALCULATE(
DISTINCTCOUNT ( Products[InvoiceNo] ) ;
ALL ( Products ) ;
Products[ProductID] = "ProdC" ;
Products[Qty] <> 0 ;
Products[InvoiceNo] = EARLIER ( Products[InvoiceNo] )
)
)
First you make sure you perform the calculation on a row level with the iterator SUMX. This makes it possible to relate to the invoice number of that particular line and compare it against a filtered version of the product table based on product C and simply count the results.

Select most recent rows in Django ORM with grouping

We have a system written in Django to track patients recruited to clinical trials.
Spread sheets are used to record the number of patients recruited each month throughout a financial year; so the sheet only contains 12 months of data even though a study may run for years.
There is a table in a django database in to which the spread sheets are imported each month. The data includes the month/year, a count of patients, and some other fields. Each import will include all the previous months data; we need this to make sure no data has been changed on the import sheet since the last import.
For example, the import table containing two imports (the first up to January and the second up to February) would look like this:
id | study_id | data_date | patient_count | [other fields] -->
100 5456 2016-04-01 10 ...
101 5456 2016-05-01 8 ...
102 5456 2016-06-01 5 ...
... all months in between ...
109 5456 2016-01-01 12 ...
110 5456 2016-02-01 NULL ...
111 5456 2016-03-01 NULL ...
112 5456 2016-04-01 10 ...
113 5456 2016-05-01 8 ...
114 5456 2016-06-01 5 ...
... all months in between ...
121 5456 2016-01-01 12 ...
122 5456 2016-02-01 6 ...
123 5456 2016-03-01 NULL ...
The other fields includes a foreign key to another table containing the actual study identification number (iras_number), so I have to join to that to select the rows for a particular study.
I want the most recent values of data_date and patient_count for a study, which may span more than one financial year, so I tried this query (iras_number is passed to the function performing this query):
totals = ImportStudyData.objects.values('data_date', 'patient_count') \
.filter(import_study__iras_number=iras_number) \
.annotate(max_id=Max('id')).order_by()
However, this produces a SQL query which includes patient_count in the GROUP BY, resulting in duplicate rows:
data_date | patient_count | max_id
2016-04-01 10 100
2016-04-01 10 112
2016-05-01 8 101
2016-05-01 8 113
...
2016-01-01 12 109
2016-01-01 12 121
2016-02-01 NULL 110
2016-02-01 6 122
How do I select the most recent data_date and patient_count from the table using the ORM?
If I were writing the SQL I would do an inner select of the max(id) grouped by data_date and then use that to join, or use an IN query, to select the fields I require from the table; such as:
SELECT data_date, patient_count
FROM importstudydata
WHERE id IN (
SELECT MAX(id) AS "max_id"
FROM importstudydata INNER JOIN importstudy
ON importstudydata.import_study_id = importstudy.id
WHERE importstudy.iras_number = 5456
GROUP BY importstudydata.data_date
)
ORDER BY data_date ASC
I've tried to create an inner select to replicate the SQL query, however the inner select returns more than one field (column) a causes the query to fail:
totals = ImportStudyData.objects.values('data_date', 'patient_count') \
.filter(id__in=ImportStudyData.objects.values('data_date') \
.filter(import_study__iras_number=iras_number) \
.annotate(max_data_id=Max('id'))
Now I can't get the inner select to return only the max(id) grouped by `data_date' and for it to be performed in a single SQL query.
For now I'm splitting the query in to a number of steps to get the result I want.
First I query for the most recent id of all rows related to the study
id_qry = ImportStudyData.objects.values('data_date')\
.filter(import_study__iras_number=iras_number)\
.annotate(max_id=Max('id'))
To get a list of just the numbers, stripping out the date, I use list comprehension:
id_list = [x['max_id'] for x in id_qry]
This list is then used as a filter for the final query to get the number of patients
totals = ImportStudyData.objects.values('data_date', 'patient_count') \
.filter(id__in=id_list)
It hits the database twice, and is computationally more expensive, but for now it works and I need to move on.
I'll come back to this problem at a later date.
Use: distinct=True
totals = ImportStudyData.objects.values('data_date', 'patient_count').filter(import_study__iras_number=iras_number).annotate(max_id=Max('id')).order_by('data_date').distinct()