BigQuery convert Month Year column to timestamp or date - google-cloud-platform

I have a table
Year_month amount
08-2021 100
09-2021 200
10-2021 300
I want to add a column Month to be
Year_month Month amount
08-2021 01-08-2021 100
09-2021 01-09-2021 200
10-2021 01-10-2021 300

It depends of the type of data you have. check below code:
with sodata as (
select '08-2021' as Year_month_string, PARSE_DATE("%m-%Y", "08-2021") as Year_month_date, 100 amount,
union all
select '09-2021',PARSE_DATE("%m-%Y", "09-2021"), 200
union all
select '10-2021',PARSE_DATE("%m-%Y", "10-2021"), 300
)
SELECT Year_month_string,FORMAT('01-%s',Year_month_string) as using_format,FORMAT_DATE("%d-%m-%Y", Year_month_date) as using_format_date,amount from sodata
output
Row
Year_month
using_format
using_format_date
amount
1
08-2021
01-08-2021
01-08-2021
100
2
09-2021
01-09-2021
01-09-2021
200
3
10-2021
01-10-2021
01-10-2021
300
To see more details about the functions used please go to this links:
Format (string)
Format Date

You can use Parse date function
SELECT PARSE_DATE("%m-%Y", "08-2021")

Related

Dax calculate a metric for another day in the calendar table

I have a table with a date column and a money column:
Thedate CashCollected
2039-12-28 100
2039-12-27 200
2039-12-26 300
2039-12-25 400
2039-12-24 500
2039-12-23 600
2039-12-22 700
2039-12-21 800
and then I have the date table with the date and the previous working day calculated following some specific conditions:
TheDate PrevItWorkDay
2039-12-28 2039-12-27
2039-12-27 2039-12-23
2039-12-26 2039-12-23
2039-12-25 2039-12-23
2039-12-24 2039-12-23
2039-12-23 2039-12-22
2039-12-22 2039-12-21
2039-12-21 2039-12-20
I would like to calculate in DAX a measure which gives me the sum of cash collected for the previous working day in the date table:
TheDate PrevItWorkDay
2039-12-28 200
2039-12-27 600
2039-12-26 600
2039-12-25 600
2039-12-24 600
2039-12-23 700
2039-12-22 800
2039-12-21
How can I do this?
PrevWorkingDaySum =
CALCULATE(
SUM(TransactionTable[CashCollected]) ,
TransactionTable[Thedate]= SELECTEDVALUE(DateTable[PrevItWorkDay])
)

SAS - Cumulative sum with date range and conditions

The following is an example of the data I have
startdate
enddate
amount
1/1/2010
2/2/2020
10
1/5/2011
2/3/2015
10
1/3/2012
2/2/2023
10
1/4/2013
2/2/2014
10
5/5/2015
2/2/2028
10
1/6/2016
2/2/2032
10
I want to calculate the sum of all existing amounts as of each start date so it should look like this:
startdate
amount
1/1/2010
10
1/5/2011
20
1/3/2012
30
1/4/2013
40
5/5/2015
30
1/6/2016
40
How do I do this in SAS?
Essentially what I want to do is for each of the start dates, calculate the cumulative sum of any amounts that haven't expired. So for the first four dates, it is just a running cumulative sum because none of the amounts have expired. But at 5/5/2015, two of the previous amounts have expired hence a cumulative sum of 30. Same for the last date, where the same two have previously expired and you have the additional amount as of 1/6/2016 therefore 40.
One way to accomplish this is with a self-join via Proc SQL:
proc sql;
create table out_dset as
select a.startdate, sum(a.amount) as amount
from in_dset as a left join in_dset as b
on a.startdate >= b.startdate and a.startdate < b.enddate
group by a.startdate
order by a.startdate;
quit;
For each observation in the original dataset, this code will find observations in the same dataset that meet the date range criteria and will sum up the amount column.
You can change the second comparison operator from < to <= if you want to include situations when a previous amount expired on the same date as a given startdate.

Measure as SUM with Min/Max Value of Another Column as Condition

I am trying to get a sum of one column based on the date in another.
Count
Date
100
05/01/2021
200
05/01/2021
300
05/01/2021
100
06/01/2021
200
06/01/2021
400
06/01/2021
100
07/01/2021
300
07/01/2021
500
07/01/2021
In SQL what I want is:
SELECT SUM([COUNT]) WHERE [Date] = MIN([Date]) FROM [rpt_data]
I can't figure out how to do this in DAX. I tried:
CALCULATE( SUM([Count]), MIN([Date]) )
But it says that I can't use MIN as a filter.
I know this works: CALCULATE( SUM([Count]), rpt_data[Date] = "07/01/2021" ) But I need to know the first and last dates dynamically.
I need another with MAX[Date]) but I figure if I get one figured out I'll figure out the other.
Currently, the engine doesn't know what do you want. We need to show which column we want to compare with a date. Example:
measure =
var __minDate = calculate( min(OrderTable[Date]))
return
CALCULATE( SUM(SalesTable[Count]), FILTER(ALL(SalesTable[Date]), SalesTable[Date] = __minDate)

Proc sql sum for each month

I have a table with dates and prices:
28/01/2016 100
04/02/2016 200
16/04/2016 100
23/04/2016 150
07/05/2016 150
14/05/2016 200
.
.
01/12/2017 80
08/12/2017 200
I would like to get a sum based on the month and then year, as follows:
01/2016 100
02/2016 200
04/2016 250
05/2016 350
.
.
12/2017 280
The date is currently in DDMMYY10. format.
Convert any individual date value within a month to a the same value and group by that. For example by using INTNX() function.
select intnx('month',date,'b') as month format=yymm7.
, sum(price) as total_price
from have
group by month
Or convert it to a string using PUT() function.
select put(date,yymm7.) as month
, sum(price) as total_price
from have
group by month

How do we Pass the multiple selected values of a Slicer inside DAX?

Let's say I have a table like this - [Orders]
Date Amount Category
12/12/2017 100 A
12/12/2017 200 B
12/12/2017 300 C
1/1/2018 400 A
1/1/2018 500 B
And I have a slicer for Category.
If only one single Value is selected, then a measure like this will work
CALCULATE(SUM(Orders[Amount]),FILTER(ALL(Orders), Orders[Category] = SelectedValue(Category))).
When more than one value is selected, how would you pass that inside the DAX Measure?
Try this:
= CALCULATE(SUM(Orders[Amount])
FILTER(ALL(Orders), Orders[Category] IN VALUES(Category)))
In most situations, you should just be able to write SUM(Orders[Amount]) and Power BI will automatically do the filtering for you based on the slicer.