AWS-quicksight: Migrate SAMEPERIODLASTYEAR from PowerbI - amazon-quicksight

I'm working with amazon-quicksight and I'm trying to migrate one funcionality from PowerBi to Quicksight but it has not been possible.
The idea is: I have a dataset with two columns "date" and "sales". The user will have a filter with the column "date". More than one date can be selected. Depending on the dates selected by the user we need to get two KPIs, the first one is the sum of sales for those dates (this is already done), but the second one is my problem, it should be the sum of the sales on the same days selected by the user but in the previous year (it depends on which year was selected for each date).
Example:
DataSet:
DATE
SALES
2020-01-05
1
2020-02-01
1
2020-06-10
4
2020-06-17
1
2021-01-01
1
2021-02-01
3
2021-06-10
3
2021-06-15
5
If the user select the dates: 2021-02-01, 2021-06-10 and 2021-06-15, the result should be:
KPI 1: Sum of sales (for those dates): 11
KPI 2: Sum of sales for those dates in the previous year: 5 -> (Days to use 2020-02-01, 2020-06-10 and 2020-06-15)
Do you have any idea about how can I calculate the KPI 2? Any suggestion?
In powerBI this (KPI 2) was done with the function: SAMEPERIODLASTYEAR
Thanks in advance.

I was able to solve this issue taking into account that the user must filter data from the two years, for example 2020 and 2021.
After that, I created the "calculated field":
calc_sum_sales = sum(sales)
After that:
calc_sum_sales_last_year = ({calc_sum_sales}-periodOverPeriodDifference(sum(sales),date,YEAR,1))
After that, I created an "visual table" and added in "group by" the field "date" and in "value" the field "calc_sum_sales_last_year"
I must give click in the column date to agroup by "year". In that way I get just two rows, one row for each year.
Finally, I change the "visual table" to "KPI" and I got the expected result.

Related

Google sheet formula to return sum based on multiple criteria using input cells

I have rows of dates with tasks measured in hours. These tasks are assigned to different team leaders whose names are also included on each row. I would like create a multiple criteria Google sheet formula that returns the sum of hours based on the date range and the name of the team leader.
These are the data input cells I would be entering to produce the sum:
Date Start:
Date End:
Team Leader Name:
Ideally if the Team Leader name was not entered, the formula would sum the hours for all of the rows selected by the date range.
Here are some sample rows:
Job Date
Hours
Team Leader
03/25/2022
8
John
04/22/2022
7
Hannah
04/23/2022
6
John
05/01/2022
6
Hannah
Thank you in advance for your help with this!
Assuming you have the following
A:C - Job Date, Hours, Team Leader
D1 - Start Date
D2 - End Date
D3 - Team Leader
=QUERY(
{A1:C},
"select Sum(Col2)
where
Col1 >= date '"&TEXT(IF(ISBLANK(D1),DATE(1970,1,1),D1),"yyyy-mm-dd")&"' and
Col1 <= date '"&TEXT(IF(ISBLANK(D2),DATE(3000,1,1),D2),"yyyy-mm-dd")&"' and
Col3 matches '"&IF(ISBLANK(D3),".*",D3)&"'
label Sum(Col2) ''")
If the start or end dates are blank, it reverts to extreme dates. For Col3 it is using REGEX -- if D3 is blank, it reverts to a wildcard and will return everything summed up.
You can use SUMIFS() function like-
=SUMIFS(B2:B,A2:A,">="&E2,A2:A,"<="&F2,C2:C,IF(G2="","*",G2))

Filter table by another table based on date field

I have a detail table in my model with a row for every hour in a day and the sales amount generated to that point of the date. Its not displayed on my report, rather it acts as the base for another reference table in the model:
Datetime Sales
2019-11-10 06:00:00 100.00
2019-11-10 12:00:00 200.00
2019-11-10 18:00:00 500.00
2019-11-10 23:59:59 999.00
The first (reference) table in the model only displays in my report the last hour value of each day in my base table. they are related via the datetime field using a 1:M with a single cross filter setting:
let
Source = Base,
#"Filtered Rows" = Table.SelectRows(Source, each ([Datetime] = #time(23, 59, 59)))
in
#"Filtered Rows"
Datetime Sales
2019-11-10 23:59:59 999.00
2019-11-11 23:59:59 950.00
2019-11-12 23:59:59 900.00
I would like to place a third table on my report that shows each of the hourly rows from the base table when a user clicks on a row in the daily table with the default setting being today's data which is added hourly.
I went down the path of using Power Query to try and filter the base table but have not been able to make it work. Should this instead be done using DAX? Either way, what would the query look like?
thanks in advance!
Thanks, but I have just resolved this. By changing my relationship to use a date field instead of the datetime field, and changing it to a (*:1) type, i am now getting the filter to do what I want. Now just have to figure out how to default to today's date. Thanks again!

How to get the difference between the values selected by slicer?

I am new to Power BI and currently I am working with table visulaizations and slicers.
My data is as follows
Student table:
Date table:
Exam table:
The relationships within the table are as follows:
I want an output like the image shown below, I would like to create 2 table visuals that can be filtered on Student Name, Classroom and also have slicer on 2 dates. I need to compute minimum score. The user must be able to select 2 dates at a time on the slicer, the first date selected on the slicer should be attached to my 'Min Score at date1' and second date selected on the slicer should be attached to my 'Min Score at date2', and the third column 'Difference in Score' must be able to calculate the difference between the Min Score at date1 and Min Score at date2.
Similarly I also want to calculate the average minimum score too
Please let me know how to proceed or what alternative formula or query or method should I apply to get the desired result.Thanks!
Before I start, let me mention that this example was done in SSAS so it may need some tweaking in PowerBi but the logic is identical nonetheless.
First create a clone date table and call it something else e.g. 'Compare Date'. Next, create an inactive, one to many relationship between the 'Compare Date' and your 'Fact' table, see the image below, in this case I am joining on [Year Month], you will need to adjust to fit your needs:
If you are unsure how to do this, just right click on the new table and select the create relationship option, ensure that the relationship is like the image below:
Once this has been done, right click on the 'relationship' and mark it as inactive.
Now that you have the new date table and the relationships set up, I want you to create a few DAX measures:
Min Date 1 = Min('Student Table'[Score])
Min Date 2 = CALCULATE(Min('Student Table'[Score]), ALL('Dates'), USERELATIONSHIP('Compare Date'[Date], 'Fact'[Date]))
Avg Date 1 = AVERAGE('Student Table'[Score])
Avg Date 2 = CALCULATE(AVERAGE('Student Table'[Score]), ALL('Dates'), USERELATIONSHIP('Compare Date'[Date], 'Fact'[Date]))
Delta Min = [Min Date 2] - [Min Date 1]
Delta Avg = [Avg Date 2] - [Avg Date 1]
These measures will calculate exactly what you need and can be filtered independently via two date slicers tied to each date table. The rest is just busy work.
I hope this helps.

PL/SQL weekly Aggregation Logic with dynamic time range

I need to aggregate the values at weekly interval. My date range is dynamic means i can give any start date and end date. Every sunday should be the starting week of every month. say if i have two columns and my start and end date is 07/11/2016 to 13/11/2016
column A column B
07/11/2016 23
08/11/2016 20
09/11/2016 10
10/11/2016 05
11/11/2016 10
12/11/2016 20
13/11/2016 10
My result should come like taking the average of column B
Column A Column B
13/11/2016 14.00
It means i should consider the past value and aggregate it to the day Sunday of that week. Also if my start and end date is like 07/11/2016 to 10/11/2016 then I should not aggregate the value as my week is not complete. I am able to aggregate the values but if my week is not complete i m not able to restrict the aggregation.
Is there any way to do this in PL/SQL??
Thank you in advance.
select to_char(columnA, 'iw') as weeknumber, avg(columnB)
from table
group by to_char(columnA, 'iw');
This will aggregate by number of week. If you need to show last day of week as a label you can get it as max(columnA) over (partition by to_char(columnA, 'iw'))

Oracle SQL group by week /month

How can we group reocrds weekly/monthly in oracle between from date and to date so that one record can be returned for one week/month.
e.g If we have 5 records for Ist weeek and 3 records for second week between a from date and to date then it should return total 2 records(one for Ist week and one for second week).
Thanks in advance.
You can group the results using GROUP BY: http://www.techonthenet.com/oracle/group_by.php
To select only those between from date and to date use WHERE
EDIT
For selecting the begining of the week or month, you can use TRUNC(the_date_field, ): http://www.techonthenet.com/oracle/functions/trunc_date.php
For example this groups by week:
SELECT TRUNC(datecol_name, 'WW') FROM table_name GROUP BY TRUNC(datecol_name, 'WW');

Resources