How to merge two tables based off key table in Power BI - merge

I have 3 csv's feeding into power BI, two containing timetracking information from 2 different services and one containing a mapping from their names to their respective ID's, as shown in the following column headings:
{Timestamp, 7P_ID, time_tracked, Work item type, ... }
{Timestamp, ZD_ID, time_tracked, ticket id, ... } (they have a few dissimilar column headers)
{employee_name, 7P_ID, ZD_ID}
I want to make a table (or at least combine this data in some way) that combines these tables so I have the following headings:
{Timestamp, employee_name, time_tracked, Work item type, ticket id, ... }
Any thoughts?
Edit: for instance...
Timestamp
7P_ID
time_tracked
work item type
2021-09-02
333-3
90
task
...
...
...
...
Timestamp
ZD_ID
time_tracked
ticket id
2021-09-03
444-444
67
24601
...
...
...
...
Employee Name
7P_ID
ZD_ID
Jeff Bozos
333-3
444-444
...
...
...
and I want...
Timestamp
Employee name
time_tracked
work item type
ticket id
2021-09-02
Jeff Bozos
90
task
2021-09-03
Jeff Bozos
67
24601
...
...
...
...
...
But of course there will be multiple employees and each will appear multiple times
Edit 2: Alternatively, it would be just as good to append a 'name' column to my two time logging files using the key file, but this is also something I can't figure out how to do.

let
T1 =
let
Source = Web.BrowserContents(
"https://stackoverflow.com/questions/68212487/how-to-merge-two-tables-based-off-key-table-in-power-bi"
),
#"Extracted Table From Html" = Html.Table(
Source,
{
{"Column1", "DIV.s-table-container:nth-child(7) > TABLE.s-table > * > TR > :nth-child(1)"},
{"Column2", "DIV.s-table-container:nth-child(7) > TABLE.s-table > * > TR > :nth-child(2)"},
{"Column3", "DIV.s-table-container:nth-child(7) > TABLE.s-table > * > TR > :nth-child(3)"},
{"Column4", "DIV.s-table-container:nth-child(7) > TABLE.s-table > * > TR > :nth-child(4)"}
},
[RowSelector = "DIV.s-table-container:nth-child(7) > TABLE.s-table > * > TR"]
),
#"Changed Type" = Table.TransformColumnTypes(
#"Extracted Table From Html",
{
{"Column1", type text},
{"Column2", type text},
{"Column3", type text},
{"Column4", type text}
}
),
#"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars = true]),
#"Changed Type1" = Table.TransformColumnTypes(
#"Promoted Headers",
{
{"Timestamp", type text},
{"7P_ID", type text},
{"time_tracked", type text},
{"work item type", type text}
}
),
#"Removed Bottom Rows" = Table.RemoveLastN(#"Changed Type1", 1),
#"Renamed Columns" = Table.RenameColumns(#"Removed Bottom Rows", {{"7P_ID", "ID"}})
in
#"Renamed Columns",
T2 =
let
Source = Web.BrowserContents(
"https://stackoverflow.com/questions/68212487/how-to-merge-two-tables-based-off-key-table-in-power-bi"
),
#"Extracted Table From Html" = Html.Table(
Source,
{
{"Column1", "DIV.s-table-container:nth-child(8) > TABLE.s-table > * > TR > :nth-child(1)"},
{"Column2", "DIV.s-table-container:nth-child(8) > TABLE.s-table > * > TR > :nth-child(2)"},
{"Column3", "DIV.s-table-container:nth-child(8) > TABLE.s-table > * > TR > :nth-child(3)"},
{"Column4", "DIV.s-table-container:nth-child(8) > TABLE.s-table > * > TR > :nth-child(4)"}
},
[RowSelector = "DIV.s-table-container:nth-child(8) > TABLE.s-table > * > TR"]
),
#"Changed Type" = Table.TransformColumnTypes(
#"Extracted Table From Html",
{
{"Column1", type text},
{"Column2", type text},
{"Column3", type text},
{"Column4", type text}
}
),
#"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars = true]),
#"Changed Type1" = Table.TransformColumnTypes(
#"Promoted Headers",
{
{"Timestamp", type text},
{"ZD_ID", type text},
{"time_tracked", type text},
{"ticket id", type text}
}
),
#"Removed Bottom Rows" = Table.RemoveLastN(#"Changed Type1", 1),
#"Renamed Columns" = Table.RenameColumns(#"Removed Bottom Rows", {{"ZD_ID", "ID"}})
in
#"Renamed Columns",
T3 =
let
Source = Web.BrowserContents(
"https://stackoverflow.com/questions/68212487/how-to-merge-two-tables-based-off-key-table-in-power-bi"
),
#"Extracted Table From Html" = Html.Table(
Source,
{
{"Column1", "DIV.s-table-container:nth-child(9) > TABLE.s-table > * > TR > :nth-child(1)"},
{"Column2", "DIV.s-table-container:nth-child(9) > TABLE.s-table > * > TR > :nth-child(2)"},
{"Column3", "DIV.s-table-container:nth-child(9) > TABLE.s-table > * > TR > :nth-child(3)"}
},
[RowSelector = "DIV.s-table-container:nth-child(9) > TABLE.s-table > * > TR"]
),
#"Changed Type" = Table.TransformColumnTypes(
#"Extracted Table From Html",
{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}}
),
#"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars = true]),
#"Changed Type1" = Table.TransformColumnTypes(
#"Promoted Headers",
{{"Employee Name", type text}, {"7P_ID", type text}, {"ZD_ID", type text}}
),
#"Removed Bottom Rows" = Table.RemoveLastN(#"Changed Type1", 1),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(
#"Removed Bottom Rows",
{"Employee Name"},
"Attribute",
"Value"
)
in
#"Unpivoted Columns",
#"T1+T2" = T1 & T2,
#"Merged Queries" = Table.NestedJoin(#"T1+T2", {"ID"}, T3, {"Value"}, "T1+T2", JoinKind.LeftOuter),
#"Expanded T1+T2" = Table.ExpandTableColumn(
#"Merged Queries",
"T1+T2",
{"Employee Name"},
{"Employee Name"}
)
in
#"Expanded T1+T2"

Related

Merging tables by date range in PowerBI

I have 2 tables:
Table 1: enrollment
student_id enrollment_date
1000 4/26/2022
1001 11/15/2022
1002 6/6/2022
1003 9/6/2022
1001 9/14/2022
1004 9/21/2022
1002 11/13/2022
1005 12/27/2022
Table 2: promotion
promo_id promotion promo_start promo_end
1 30% Promo 9/5/2022 9/26/2022
2 Christmas Promotion 12/25/2022 12/31/2022
3 11.11 Promotion 11/11/2022 11/15/2022
What I want to achieve is to merge 2 tables together based on date range defined in promotion table. If the enrollment_date did not fall into any promotion period, then just leave it as null. Below is the expected output:
Any advise or help will be greatly appreciated!
Here you go.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RYxBCoAgEEWvIgPtIv0jBq27QHtp0a4WJaj3J0lz/u495o33BFVHI1kzqC2GO6i+YhftNBvmn3j+kPbRE8vZesYr5ftI9UW+wlMsWHPLK1lIbaUGJkDKbjUgdSHX6v0F", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [promo_id = _t, promotion = _t, promo_start = _t, promo_end = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"promo_id", Int64.Type}, {"promotion", type text}, {"promo_start", type text}, {"promo_end", type text}}),
#"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"promo_end", type date},{"promo_start", type date}}, "en-US")
in
#"Changed Type with Locale"
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dc7LCcAwDAPQVYrPAVnKp2SW0P3XKA0lrQ/x9SHJYxjd/ZhnyQrUIJfsSpO4iARrMC1riKm8pGPX18ESqPxI3Cw9X+Rg9TNB52vXDQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [student_id = _t, enrollment_date = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"student_id", Int64.Type}, {"enrollment_date", type text}}),
#"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"enrollment_date", type date}}, "en-US"),
#"Added Custom" = Table.AddColumn(#"Changed Type with Locale", "promo_id", (x)=> Table.SelectRows(promotion, (y)=> x[enrollment_date] >= y[promo_start] and x[enrollment_date] <= y[promo_end]){0}?[promo_id]?)
in
#"Added Custom"

Power Query how to get listed rows from the table for the Min or Max Date?

Power Query how to get listed rows from the table for the Min or Max Date?
Here is the code that is currently not working
let Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Project ID", type text}, {"Activity ID", type text}, {"Activity Name", type text}, {"System Locator-g", type text}, {"Milestone-g", type text}, {"Original Duration", Int64.Type}, {"Test Work Type-g", type text}, {"MXO_WONUM", type text}, {"Start", type any}, {"Finish", type any}, {"Activity Status", type text}, {"MXO_EQUIPTAGS", type any}, {"Discipline-g", type any}, {"Restraints", type any}, {"Empty row", type any}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"MXO_WONUM"}, #"Append1 Loc Max Dump & Loc Max Dump", {"wonum"}, "Append1 Loc Max Dump & Loc Max Dump", JoinKind.LeftOuter),
#"Merged Queries1" = Table.NestedJoin(#"Renamed Columns", {"Milestone-g"}, #"Milestone Sequence", {"Milestone-g"}, "Milestone Sequence", JoinKind.LeftOuter),
#"Expanded Milestone Sequence" = Table.ExpandTableColumn(#"Merged Queries1", "Milestone Sequence", {"Milestone Sequence"}, {"Milestone Sequence.Milestone Sequence"}),
#"Renamed Columns1" = Table.RenameColumns(#"Expanded Milestone Sequence",{{"Milestone Sequence.Milestone Sequence", "Milestone Sequence"}}),
#"Merged Queries2" = Table.NestedJoin(#"Changed Type3", {"Component"}, #"CTM P6 Update Tab (2) - unpivoted", {"Component"}, "CTM P6 Update Tab (2) - unpivoted", JoinKind.LeftOuter),
#"Expanded CTM P6 Update Tab (2) - unpivoted" = Table.ExpandTableColumn(#"Merged Queries2", "CTM P6 Update Tab (2) - unpivoted", {"Test Status "}, {"CTM P6 Update Tab (2) - unpivoted.Test Status "}),
#"Renamed Columns2" = Table.RenameColumns(#"Expanded CTM P6 Update Tab (2) - unpivoted",{{"CTM P6 Update Tab (2) - unpivoted.Test Status ", "Test Status"}}),
#"Grouped Rows" = Table.Group(#"Renamed Columns2", {"Component", "Milestone-g", "Milestone Sequence", "Activity Status", "Activity ID", "Activity Name"}, {{"Grouped", each Table.FirstN(Table.Sort(#"Renamed Columns2",{"Grouped",Order.Ascending}),1), type table}})
in #"Grouped Rows"
Are you trying to get the rows that are either the min or max date of the Grouped column?
#"Filtered Rows" = Table.SelectRows(#"YourPriorStepNameGoesHere", each ([Grouped] = List.Min(#"YourPriorStepNameGoesHere"[Grouped]) or [Grouped] =List.Max(#"YourPriorStepNameGoesHere"[Grouped])))
Edit per question:
to do this by Component, best way is to group on Component then expand. I am assuming one row per date, otherwise I'd have to do it another way
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Component", type text}, {"Grouped", type date}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Component"}, {{"data", each
Table.FirstN(Table.Sort(_,{{"Grouped", Order.Ascending}}),1) &
Table.LastN(Table.Sort(_,{{"Grouped", Order.Ascending}}),1)
, type table }}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Grouped"}, {"Grouped"})
in #"Expanded data"

Merge queries matching on dates and leave null if no match found

I have got two tables below (examples):
Table SFID
Sales Force ID
Type
Name
Assistant
From
To
123
ABC
Store A
Ben
01/04/2020
30/04/2020
123
ABC
Store A
Jen
01/05/2020
31/05/2020
123
ABC
Store A
Ben
01/06/2020
21/06/2020
126
DEF
Store B
Tim
01/04/2020
30/04/2020
126
DEF
Store B
Tim
01/04/2020
null
and
Table Activity
Transaction ID
Date
Sales Force ID
1
03/05/2020
123
2
03/06/2020
200
3
01/01/2021
123
4
02/01/2021
126
I want my end result to be
Transaction ID
Date
Sales Force ID
Type
Name
Assistant
1
03/05/2020
123
ABC
Store A
Jen
2
03/06/2020
200
null
null
null
3
01/01/2021
123
null
null
null
4
02/01/2021
126
DEF
Store B
Tim
To do this, the best solution was the one posted in here with some modifications (allow both To and From to be null). However, only the row on transaction ID 2 disappears because that Sales Force had already had assistant entries (they get wiped out on the filtered row step). I also tried the solution presented in here but it takes ages to load.
I would like to know if there was a way to guarantee all transactions appear without having to introduce lines to table SFID for periods they don't have assistants and without making the query a really slow one.
This is my code:
let
Source = Source,
#"Merged Queries" = Table.NestedJoin(Source,{"Sales Force ID"},SFID,{"SFID"},"SFID",JoinKind.LeftOuter),
#"Expanded SFID" = Table.ExpandTableColumn(#"Merged Queries", "SFID", {"Type", "Name", "Assistant", "From", "To"}, {"Type", "Name", "Assistant", "From", "To"}),
#"Changed Type" = Table.TransformColumnTypes(#"SFID",{{"Date", type date}, {"From", type date}, {"To", type date}}),
FilteredRows = Table.SelectRows(#"Changed Type", each ([Date] >= [From] and [Date] <= [To]) or ([Date] >= [From] and [To] = null)or ([From] = null and [To] = null)),
#"Removed Columns" = Table.RemoveColumns(FilteredRows,{"From", "To"})
in
#"Removed Columns"
So after almost 2 weeks with no response and an unjustified downgrade, I found a solution!
I created a query that would basically produce this table with the above mentioned code.
Transaction ID
Date
Assistant
1
01/01/2021
Jen
4
02/01/2021
Tim
let
Source = Source,
#"Merged Queries" = Table.NestedJoin(Source,{"Sales Force ID"},SFID,{"SFID"},"SFID",JoinKind.LeftOuter),
#"Expanded SFID" = Table.ExpandTableColumn(#"Merged Queries", "SFID", {"Type", "Name", "Assistant", "From", "To"}, {"Type", "Name", "Assistant", "From", "To"}),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded SFID",{{"Date", type date}, {"From", type date}, {"To", type date}}),
FilteredRows = Table.SelectRows(#"Changed Type", each ([Date] >= [From] and [Date] <= [To]) or ([Date] >= [From] and [To] = null)),
#"Removed Columns" = Table.RemoveColumns(FilteredRows,{"From", "To"})
in
#"Removed Columns"
And left-joined it on my initial version of Table Activity
I kept the information of type Type and Name in a separate Query (as they don't change) and then again left-joined it on the Table Activity.
Final query looks like this:
let
Source = Source,
#"Merged Queries1" = Table.NestedJoin(Source,{"Sales Force Code"},Info,{"SFID"},"Info",JoinKind.LeftOuter),
#"Expanded Info" = Table.ExpandTableColumn(#"Merged Queries1", "Info", {"Type", "Name"}, {"Type", "Name"}),
#"Merged Queries" = Table.NestedJoin(#"Expanded Info",{"ID"},IDvsAssistant,{"ID"},"IDvsAssistant",JoinKind.LeftOuter),
#"Expanded IDvsAssistant" = Table.ExpandTableColumn(#"Merged Queries", "IDvsAssistant", {"Assistant"}, {"Assistant"})
in
#"Expanded IDvsAssistant"

Remove first column after "group by All rows" operationin Power Query

I have an issue when trying to group by first column Podrocje_dela (PHASE 1). I use Group by All rows where second column has a name Aktivnosti.
PHASE 1
as the result is in two column format (PHASE 2), but I would like only last column, so the one marked with blue pen should be removed.
PHASE 2
Why this is a problem? I need a first column items as column Names (red), but this names also appear as column values (blue) combined from 1st and 2nd column (PHASE 3), comma separated. I would need only second column values as shown in last image (PHASE 4).
PHASE 3
End result should be like is shown in image below (PHASE 4). This result is accomplished by .AfterDelimiter operation but as the number of different first column items (PHASE 1) can change it is not flexible enough.
PHASE 4
For the end, I need result as shown in PHASE 4 from PHASE 1. Maybe my approach is not the best and I would be happy for any suggestions.
My current code is
let
Source = Access.Database(File.Contents("\\fs-srv01\PROJEKTI\S-DELOVNI-PROCES\DELOVNE URE\MS_Access\Podatkovne_baze\Organizacija.accdb"), [CreateNavigationProperties=true]),
_tblPodrocja_dela = Source{[Schema="",Item="tblPodrocja_dela"]}[Data],
#"Removed Columns" = Table.RemoveColumns(_tblPodrocja_dela,{"tblPodrocjaDela_Aktivnosti"}),
#"Merged Queries" = Table.NestedJoin(#"Removed Columns",{"ID_Podrocje_dela"},tblPodrocjaDela_Aktivnosti_M2M,{"ID_Podrocje_dela"},"tblPodrocjaDela_Aktivnosti_M2M",JoinKind.FullOuter),
#"Expanded tblPodrocjaDela_Aktivnosti_M2M" = Table.ExpandTableColumn(#"Merged Queries", "tblPodrocjaDela_Aktivnosti_M2M", {"ID_Aktivnost"}, {"tblPodrocjaDela_Aktivnosti_M2M.ID_Aktivnost"}),
#"Merged Queries1" = Table.NestedJoin(#"Expanded tblPodrocjaDela_Aktivnosti_M2M",{"tblPodrocjaDela_Aktivnosti_M2M.ID_Aktivnost"},tblAktivnostiVsakodnevne,{"ID_aktivnost"},"tblAktivnosti",JoinKind.LeftOuter),
#"Expanded tblAktivnosti" = Table.ExpandTableColumn(#"Merged Queries1", "tblAktivnosti", {"Aktivnost"}, {"tblAktivnosti.Aktivnost"}),
#"Removed Columns1" = Table.RemoveColumns(#"Expanded tblAktivnosti",{"ID_Podrocje_dela", "tblPodrocjaDela_Aktivnosti_M2M.ID_Aktivnost"}),
#"Grouped Rows" = Table.Group(#"Removed Columns1", {"Podrocje_dela"}, {{"Aktivnosti", each _, type table}}),
toList = Table.TransformColumns(#"Grouped Rows", {"Aktivnosti", Table.ToList}),
output = #table(toList[Podrocje_dela],List.Zip(toList[Aktivnosti])),
#"Extracted Text After Delimiter" = Table.TransformColumns(output, {{"Finance", each Text.AfterDelimiter(_, ","), type text}, {"IT", each Text.AfterDelimiter(_, ","), type text}, {"Kadrovska", each Text.AfterDelimiter(_, ","), type text}, {"Komerciala", each Text.AfterDelimiter(_, ","), type text}, {"Podporne službe", each Text.AfterDelimiter(_, ","), type text}, {"Proizvodnja", each Text.AfterDelimiter(_, ","), type text}, {"Raziskave in razvoj", each Text.AfterDelimiter(_, ","), type text}, {"Vodstvo", each Text.AfterDelimiter(_, ","), type text}})
Thank you all!
If anybody has the same problem. I found a solution to my problem. To have only single column grouped I specified which column to be specified:
from
#"Grouped Rows" = Table.Group(#"Removed Columns1", {"Podrocje_dela"}, {{"Aktivnosti", each _, type table}}),
to
#"Grouped Rows" = Table.Group(#"Removed Columns1", {"Podrocje_dela"}, {{"Aktivnosti", each _[Aktivnosti], type table}}),
The Power Query code with Ascending sort where nulls are at the end:
let
Source = Access.Database(File.Contents("\\fs-srv01\PROJEKTI\S-DELOVNI-PROCES\DELOVNE URE\MS_Access\Podatkovne_baze\Organizacija.accdb"), [CreateNavigationProperties=true]),
_tblPodrocjaDela_Aktivnosti = Source{[Schema="",Item="tblPodrocjaDela_Aktivnosti"]}[Data],
#"Removed Columns" = Table.RemoveColumns(_tblPodrocjaDela_Aktivnosti,{"ID_Podrocje_dela", "ID_Aktivnost"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"tblPodrocja_dela", "tblAktivnosti"}),
#"Expanded tblPodrocja_dela" = Table.ExpandRecordColumn(#"Reordered Columns", "tblPodrocja_dela", {"Podrocje_dela"}, {"Podrocje_dela"}),
#"Expanded tblAktivnosti" = Table.ExpandRecordColumn(#"Expanded tblPodrocja_dela", "tblAktivnosti", {"Aktivnost", "Vsakodnevna"}, {"Aktivnosti", "tblAktivnosti.Vsakodnevna"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded tblAktivnosti", each true),
#"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Aktivnosti", Order.Ascending}}),
#"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows",{"tblAktivnosti.Vsakodnevna"}),
#"Grouped Rows" = Table.Group(#"Removed Columns1", {"Podrocje_dela"}, {{"Aktivnosti", each _[Aktivnosti], type table}}),
toList = Table.Sort(#"Grouped Rows",{{"Podrocje_dela", Order.Ascending}}),
MyColumnNames = toList[Podrocje_dela],
arrangedDataFromAccess = #table(MyColumnNames ,List.Zip(toList[Aktivnosti])),
SortAllColumns = Table.FromColumns(List.Generate(()=>
[x=0,y=Table.Column(arrangedDataFromAccess ,MyColumnNames{x})],
each [x] < List.Count(MyColumnNames),
each [x=[x]+1,y=Table.Column(arrangedDataFromAccess ,MyColumnNames{x})],
each List.Sort([y] ,(xx,yy)=>if xx&yy<> null then Value.Compare(xx ,yy) else Value.Compare(yy ,xx))),MyColumnNames),
CountItemsInColumns = List.Generate(()=>
[x=0,y=Table.Column(arrangedDataFromAccess ,MyColumnNames{x})],
each [x] < List.Count(MyColumnNames),
each [x=[x]+1,y=Table.Column(arrangedDataFromAccess ,MyColumnNames{x})],
each List.NonNullCount([y])),
CountItemsInColumnsTransposed = Table.Transpose(Table.FromList(CountItemsInColumns,Splitter.SplitByNothing()), MyColumnNames),
finalDataFromAccess = Table.Combine({CountItemsInColumnsTransposed, SortAllColumns}) in finalDataFromAccess

Convert date in jsonb (Postgres)

I have jsonb column data =
"{"history": [{"endDate": "30.06.2015", "classname": "Class A", "startDate": "2010-04-01", "numberAction": "0016", "positionName": "Teacher"},
{"endDate": "31.06.2010", "classname": "Class A", "startDate": "2005-08-10", "numberAction": "0015", "positionName": "Student"},
{"endDate": "2005.08.09", "classname": "Class B", "startDate": "2005-02-21", "numberAction": "0014", "positionName": " Student "}]}"
As you can see, the dates of the "endDate" in the array are not correct. Please tell me how they can be converted to the format YYYY-MM-DD?
My idle attempt:
UPDATE table
SET data = jsonb_set(data, '{endDate}', to_date('{endDate}', 'YYYY-MM-DD'), false)
WHERE id = 'UUID';
Answer =
update table
set data = data - 'history' ||
jsonb_build_object
( 'history'
, ( select jsonb_agg
( case when aa.value ->> 'endDate' like '%.%' then
aa.value - 'endDate' || jsonb_build_object
( 'endDate'
, to_date(aa.value ->> 'endDate','dd.mm.yyyy')
)
else
aa.value
end
)
from jsonb_array_elements(data -> 'history') as aa
)
)
WHERE uuid = 'UUID'
and exists ( select *
from jsonb_array_elements(data -> 'history') as aa
where aa.value ->> 'endDate' like '%.%'
);