power query - expanding new columns - merge

i have two table to merge and then i expand newcolumns. the problem is the first table can have new columns as we enter new data to a new month/year. the result does not include these new months.
is there a way to manage to expand new columns while merging ?
the queries :
let
Source = #"320 Odemeler",
#"Merged Queries" = Table.NestedJoin(Source,{"HESAP NO"},#"320 Faturalar",{"HESAP NO"},"NewColumn",JoinKind.LeftOuter),
#"Expanded NewColumn" = Table.ExpandTableColumn(#"Merged Queries", "NewColumn", {"HESAP NO", "HESAP ADI", "Nisan - 2016", "Haziran - 2016", "Temmuz - 2016", "Ağustos - 2016", "Eylül - 2016", "Ekim - 2016", "Kasım - 2016", "Aralık - 2016", "Ocak - 2017", "Şubat - 2017", "Mart - 2017", "Mayıs - 2017"}, {"HESAP NO.1", "HESAP ADI.1", "Nisan - 2016", "Haziran - 2016", "Temmuz - 2016", "Ağustos - 2016", "Eylül - 2016", "Ekim - 2016", "Kasım - 2016", "Aralık - 2016", "Ocak - 2017", "Şubat - 2017", "Mart - 2017", "Mayıs - 2017"})
in
#"Expanded NewColumn"
the other query :
let
Source = #"Tum Hareketler",
#"Sorted Rows" = Table.Sort(Source,{{"DATE_", Order.Ascending}}),
#"Duplicated Column" = Table.DuplicateColumn(#"Sorted Rows", "DATE_", "DATE_ - Copy"),
#"Extracted Month Name" = Table.TransformColumns(#"Duplicated Column", {{"DATE_", each Date.MonthName(_, "tr-TR"), type text}}),
#"Extracted Year" = Table.TransformColumns(#"Extracted Month Name",{{"DATE_ - Copy", Date.Year}}),
#"Added Custom" = Table.AddColumn(#"Extracted Year", "Tarih", each [DATE_] & " - " & Number.ToText([#"DATE_ - Copy"], "G","")),
#"Grouped Rows" = Table.Group(#"Added Custom", {"HESAP NO", "HESAP ADI", "Tarih"}, {{"Sum Alacak", each List.Sum([ALACAK]), type number}}),
#"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Tarih]), "Tarih", "Sum Alacak", List.Sum)
in
#"Pivoted Column"

First I converted "Tarih" List to Table,
Then managed to write the below query :
let
Liste = Tarih,
TarihListe = Liste [Column1],
Source = #"320 Odemeler",
#"Merged Queries" = Table.NestedJoin(Source,{"HESAP NO"},#"320 Faturalar",{"HESAP NO"},"NewColumn",JoinKind.LeftOuter),
#"Expanded NewColumn" = Table.ExpandTableColumn(#"Merged Queries", "NewColumn", TarihListe , TarihListe )
in
#"Expanded NewColumn"
So, every time a new month column added by the Pivot Column, I don't have to edit the query to add it to the query...

Related

Power Query Convert Date (type date) to serial number

I have a column in power query called "Delivery Date", type date.
I want to convert the date to serial number (the same as in excel when you change type date to "general" format. My date is "yyyy-mm-dd"
example:
"2023-02-01" formatted as "44958"
"2023-01-31" formatted as "44957"
This is the output I would like:
Delivery Date
DateCode
2023-02-01
44958
2023-01-31
44957
I've tried this code:
= Table.TransformColumnTypes(Source,{{"Delivery Date", Number.From}})
as well as duplicating my "Delivery Date" column and changing Type to "Int64"
in both cases my output is 6.38108E+17 for Feb 1st date, and 6.38107E+17 for Jan 31st date.
I have not been able to find a solution anywhere online thus far and have been formatting it once it's loaded into excel (which is an unnecessary step).
Thanks!
How about
let Source = #table({"Delivery Date"},{{"2023-02-01 5:00:00 AM +00:00"}}),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Delivery Date", type datetime}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Delivery Date", type number}})
in #"Changed Type1"
Transform to date/time
Transform again to number, without replacing original transformation

Power BI date condition

I have two date fields
"MonthEndDate" and "date" I want a condition to satisfy that if date is less than 15 then take previous month end date and if date is more than 15 then take current month end date. Eg-
MonthEndDate
date
ResultDate
6/30/1999
7/2/1999
6/30/1999
10/31/1999
11/1/1999
10/31/1999
5/31/2009
5/28/2009
5/31/2009
You can achieve this using power query
let
Source = Excel.Workbook(File.Contents("C:\Ashok\Power BI\Stack Overflow\data_08_nov_21.xlsx"), null, true),
Data_Sheet = Source{[Item="Data",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Data_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"MonthEndDate ", type date}, {" date ", type date}}),
#"Inserted Day" = Table.AddColumn(#"Changed Type", "Day", each Date.Day([#" date "]), Int64.Type),
#"Added Custom" = Table.AddColumn(#"Inserted Day", "Custom", each if[Day] < 15
then Date.EndOfMonth( Date.AddMonths([#" date "], -1))
else Date.EndOfMonth([#" date "])),
#"Renamed Columns" = Table.RenameColumns(#"Added Custom",{{"Custom", "ResultDate"}})
in
#"Renamed Columns"

Dynamic date pivot using CTE

I have a CTE which i get after lot of calculations, So the final CTE contains three columns
namely, "Name", "Values" & "Dates".
I have searched google and in all PIVOT the dates are hardcoded and i don't wan this dates to be hardcoded as the dates will be dynamically changed.
Any suggestions how i can get this result with a CTE and with dynamic dates pivot?
Version PostgreSQL 11
Sample input
Value. Name Date
"1" "Coenzym Q10" "2020-06-29"
"1" "Coenzym Q10" "2020-06-30"
"4" "Coenzym Q10" "2020-07-01"
"1" "Coenzym Q10" "2020-07-02"
"5" "Coenzym Q10" "2020-07-03"
"1" "Coenzym Q10" "2020-07-04"
"2" "D12" "2020-07-01"
"4" "D12" "2020-07-04"
Desired Output
"Name" "2020-07-04". "2020-07-03" "2020-07-02" "2020-07-01" "2020-06-30". "2020-06-29"
"Coenzym Q10". "4" "5" "1" "1" "1" "1"
"D12" "4" "2"
If you don't insist of having the dates as the column name (header), you could do something like this:
select "Name",
max("Value") filter (where date = current_date) as "Today",
max("Value") filter (where date = current_date - 1) as "Today -1",
max("Value") filter (where date = current_date - 2) as "Today -2",
max("Value") filter (where date = current_date - 3) as "Today -3",
max("Value") filter (where date = current_date - 4) as "Today -4",
max("Value") filter (where date = current_date - 5) as "Today -5",
max("Value") filter (where date = current_date - 6) as "Today -6",
max("Value") filter (where date = current_date - 7) as "Today -7"
from the_table
group by "Name"
order by "Name;

Is there a formula for upcoming date using formula in Google sheet based on condition?

I have a google sheet where I need to get the next upcoming date based on the start date set in column A
Any pointers are greatly appreciated? I am unable exhibit the efforts as I am completely new to this sort of recurrence using Google sheets
https://docs.google.com/spreadsheets/d/1g_UNg4MjDy3gFufpjZtMRkbBz80K3scQdZOaiAnZi7I/edit#gid=0
This behavior (the next date from today including today) could be implemented manually by this formula:
={
"Next date from today";
ARRAYFORMULA(
IFS(
A2:A >= TODAY(),
A2:A,
B2:B = "Daily",
TODAY() + MOD(TODAY() - A2:A, C2:C),
B2:B = "Weekly",
TODAY() + MOD(TODAY() - A2:A, 7 * C2:C),
B2:B = "Monthly",
EDATE(A2:A, ROUNDUP((12 * (YEAR(TODAY()) - YEAR(A2:A)) + (MONTH(TODAY()) - MONTH(A2:A)) - IF(DAY(TODAY()) < DAY(A2:A), 1, 0)) / C2:C, 0) * C2:C),
True,
""
)
)
}
For additional options (like "every 2nd monday of the month" and others) additional options should be implemented in that IFS part.
If you are interested in a trivial case where the next date from the start date (column F:F on the screenshot) is needed, then the formula would be much simpler:
={
"Next date";
ARRAYFORMULA(
IFS(
B2:B = "Daily",
A2:A + C2:C,
B2:B = "Weekly",
A2:A + 7 * C2:C,
B2:B = "Monthly",
EDATE(A2:A, C2:C),
True,
""
)
)
}
Again, for additional options you'll need to add corresponding part to the IFS.
You could use IFS to check Frequency, and:
If Daily, add Count value to start date.
If Weekly, add Count value multiplied by 7.
If Monthly, since not all months have the same duration, retrieve the YEAR, MONTH and DAY indexes, add Count to the MONTH index, and set a new DATE, EDIT: or as suggested by kishkin, use EDATE.
It could be something like this:
=ARRAYFORMULA(IFNA(IFS(
B2:B = "Daily", A2:A + C2:C,
B2:B = "Weekly", A2:A + 7 * C2:C,
B2:B = "Monthly", EDATE(A2:A,C2:C)
)))

Convert text date (March 21st 2020) to dd/mm/yyyy

I've imported an excel sheet into PowerBI and I'm trying to trying to transform the date column from text format (March 21st 2020, 00:00:000) into date format (21/03/2020).
Unfortunately when I select "detect type" it only selects text, and selecting "Date" presents an error. I've removed the ", 00:00:000" from the columns but still receiving an error message.
Does anyone have any suggestions on how I can transform this column into dd/mm/yyyy?
Many thanks,
After removing the , 00:00:000, you can use a transformation like one of these to remove the st part in 21st so that March 21st 2020 --> March 21 2020, which can be automatically converted to a date.
This transformation strips all characters except space and numbers after the first space:
each Text.BeforeDelimiter(_, " ") & Text.Select(_, {" ", "0".."9"})
This one deletes the two characters to the left of the last space:
each Text.ReplaceRange(_, Text.PositionOf(_, " ", Occurrence.Last)-2, 2, "")
The full query (using the first option) would look like this:
let
Source = <Your Data Source>,
Remove00 = Table.TransformColumns(Source, {{"DateText", each Text.BeforeDelimiter(_, ","), type text}}),
StripOrdinal = Table.TransformColumns(Remove00, {{"DateText", each Text.BeforeDelimiter(_," ") & Text.Select(_, {" ","0".."9"}), type text}}),
TextToDate = Table.TransformColumnTypes(StripOrdinal,{{"DateText", type date}})
in
TextToDate
You could use this PowerQuery Script:
let
Source = Excel.Workbook(File.Contents("Dates.xlsx"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Sheet1_Sheet,{{"Column1", type text}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Column1", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3"}),
#"Added Month" = Table.AddColumn(#"Split Column by Delimiter", "Month", each if [Column1.1] = "January" then 1 else if [Column1.1] = "February" then 2 else if [Column1.1] = "March" then 3 else if [Column1.1] = "April" then 4 else if [Column1.1] = "May" then 5 else if [Column1.1] = "June" then 6 else if [Column1.1] = "July" then 7 else if [Column1.1] = "August" then 8 else if [Column1.1] = "September" then 9 else if [Column1.1] = "October" then 10 else if [Column1.1] = "November" then 11 else if [Column1.1] = "December" then 12 else 0),
#"Added Day" = Table.AddColumn(#"Added Month", "Day", each Text.Select([Column1.2], {"0".."9"})),
#"Removed Columns" = Table.RemoveColumns(#"Added Day",{"Column1.1", "Column1.2"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Column1.3", "Year"}}),
#"Changed Type2" = Table.TransformColumnTypes(#"Renamed Columns",{{"Year", Int64.Type}, {"Month", Int64.Type}, {"Day", Int64.Type}}),
#"Inserted Merged Column" = Table.AddColumn(#"Changed Type2", "Date", each Text.Combine({Text.From([Day], "de-DE"), "/", Text.From([Month], "de-DE"), "/", Text.From([Year], "de-DE")}), type text),
#"Changed Type3" = Table.TransformColumnTypes(#"Inserted Merged Column",{{"Date", type date}}),
#"Removed Columns2" = Table.RemoveColumns(#"Changed Type3",{"Year", "Month", "Day"})
in
#"Removed Columns2"
The conversion to your final data format is depending on your local settings and is usally done outside PowerQuery.