Forum Discussion
Date.AddDays Loop
- 3 years ago
Hi , Anonymous
Thanks for your quick response!
I cannot connect to your data source, so you need to test in your side .
According to your M code provided, you can try to use this M code to put in the "Advanced Editor":
let Source = MySQL.Database("34.252.51.71", "powerbi114", [ReturnSingleDatabase=true]), powerbi114_cmCarerAvailability = Source{[Schema="powerbi114",Item="cmCarerAvailability"]}[Data], #"Replaced Value" = Table.ReplaceValue(powerbi114_cmCarerAvailability,",","",Replacer.ReplaceText,{"day"}), #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"day", Int64.Type}}), #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"created", "createdBy", "deleted", "deletedBy", "updated", "updatedBy", "companyId", "locationId", "adjustedStartDate", "startTime", "endTime"}), #"Added Custom" = Table.AddColumn(#"Removed Columns", "start_Monday", (x)=>Date.AddDays(Date.StartOfWeek(x[startDate]),1)), #"Added Custom1" = Table.AddColumn(#"Added Custom", "Accumul_list", (x)=> if x[week]=0 then null else if x[week]=1 then {0..(Duration.Days(Date.AddDays(Date.EndOfWeek(x[endDate]),1)-x[start_Monday])+1)/7-1}else if x[week]=2 then List.Generate(()=>0,each _<=(Number.RoundUp((Duration.Days(Date.AddDays(Date.EndOfWeek(x[endDate]),1)-x[start_Monday])+1)/7/2)-1)*2,each _+2) else 1), #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Need List", (x)=> if x[Accumul_list]<> null then List.Sort(List.TransformMany(x[day_list],each x[Accumul_list],(x,y)=>y*7+x)) else x[endDate] ), #"Added Custom3" = Table.AddColumn(#"Added Custom2", "End List", (x)=> try List.Select(List.Accumulate(x[Need List],{},(a,b)=> a& {Date.AddDays(Date.From(x[start_Monday]) ,b)} ),(z)=>z<=x[endDate]) otherwise x[Need List] ) in #"Added Custom3"If this also return the error code , you can share the error code to me to update this M code.
Thank you for your time and sharing, and thank you for your support and understanding of PowerBI!
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly
Hi, Anonymous
Thanks for your quick response, the last two days were weekend so i can not response you immediately.
For your question, can you connect your data source from Mysql and make the return table like this:
Then you can give me all the screenshots of your Power Query Editor interface(I want to see the column whether is the same as me ), including your fields and the steps you generated.
And you need to copy the whole M code in the "Advanced Editor" of this query in Power Query Editor to me.
If you can, you can also upload the. pbix file to OneDrive and share your link with me.
Thank you for your time and sharing, and thank you for your support and understanding of PowerBI!
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly
v-yueyunzh-msft TY for your asistance to date.
My Advanced Editor has the below code.
let
Source = MySQL.Database("34.252.51.71", "powerbi114", [ReturnSingleDatabase=true]),
powerbi114_cmCarerAvailability = Source{[Schema="powerbi114",Item="cmCarerAvailability"]}[Data],
#"Replaced Value" = Table.ReplaceValue(powerbi114_cmCarerAvailability,",","",Replacer.ReplaceText,{"day"}),
#"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"day", Int64.Type}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"created", "createdBy", "deleted", "deletedBy", "updated", "updatedBy", "companyId", "locationId", "adjustedStartDate", "startTime", "endTime"}),
#"Added Custom" = Table.AddColumn(
test, "start_Monday", (x)=>Date.AddDays(Date.StartOfWeek(x[startDate]),1)
),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Accumul_list", (x)=>
if x[week]=0 then null else if x[week]=1 then {0..(Duration.Days(Date.AddDays(Date.EndOfWeek(x[endDate]),1)-x[start_Monday])+1)/7-1}else if x[week]=2 then
List.Generate(()=>0,each _<=(Number.RoundUp((Duration.Days(Date.AddDays(Date.EndOfWeek(x[endDate]),1)-x[start_Monday])+1)/7/2)-1)*2,each _+2) else 1),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Need List", (x)=>
if x[Accumul_list]<> null then List.Sort(List.TransformMany(x[day_list],each x[Accumul_list],(x,y)=>y*7+x)) else x[endDate]
),
#"Added Custom3" = Table.AddColumn(#"Added Custom2", "End List", (x)=>
try
List.Select(List.Accumulate(x[Need List],{},(a,b)=> a& {Date.AddDays(Date.From(x[start_Monday]) ,b)} ),(z)=>z<=x[endDate])
otherwise x[Need List]
)
in
#"Added Custom3"
PBIX -> https://1drv.ms/u/s!AiX0_cACeSxJgSqVYOQ7GmGUY8FG?e=dH6kJG
- v-yueyunzh-msft3 years agoCommunity Support
Hi , Anonymous
Thanks for your quick response!
I cannot connect to your data source, so you need to test in your side .
According to your M code provided, you can try to use this M code to put in the "Advanced Editor":
let Source = MySQL.Database("34.252.51.71", "powerbi114", [ReturnSingleDatabase=true]), powerbi114_cmCarerAvailability = Source{[Schema="powerbi114",Item="cmCarerAvailability"]}[Data], #"Replaced Value" = Table.ReplaceValue(powerbi114_cmCarerAvailability,",","",Replacer.ReplaceText,{"day"}), #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"day", Int64.Type}}), #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"created", "createdBy", "deleted", "deletedBy", "updated", "updatedBy", "companyId", "locationId", "adjustedStartDate", "startTime", "endTime"}), #"Added Custom" = Table.AddColumn(#"Removed Columns", "start_Monday", (x)=>Date.AddDays(Date.StartOfWeek(x[startDate]),1)), #"Added Custom1" = Table.AddColumn(#"Added Custom", "Accumul_list", (x)=> if x[week]=0 then null else if x[week]=1 then {0..(Duration.Days(Date.AddDays(Date.EndOfWeek(x[endDate]),1)-x[start_Monday])+1)/7-1}else if x[week]=2 then List.Generate(()=>0,each _<=(Number.RoundUp((Duration.Days(Date.AddDays(Date.EndOfWeek(x[endDate]),1)-x[start_Monday])+1)/7/2)-1)*2,each _+2) else 1), #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Need List", (x)=> if x[Accumul_list]<> null then List.Sort(List.TransformMany(x[day_list],each x[Accumul_list],(x,y)=>y*7+x)) else x[endDate] ), #"Added Custom3" = Table.AddColumn(#"Added Custom2", "End List", (x)=> try List.Select(List.Accumulate(x[Need List],{},(a,b)=> a& {Date.AddDays(Date.From(x[start_Monday]) ,b)} ),(z)=>z<=x[endDate]) otherwise x[Need List] ) in #"Added Custom3"If this also return the error code , you can share the error code to me to update this M code.
Thank you for your time and sharing, and thank you for your support and understanding of PowerBI!
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly