r/ExcelPowerQuery • • Jul 08 '25

I really need help with this Power Query...

Hi all

(I’ve whitened out some data as this is company sensitive)

So, the issue i have is that i need to create a ‘simple’ planning on what day we can earliest receive containers at our company with the only rule that we can only receive 15 containers each day.

I took the following steps in Power query:

  • What i already did was create an additional column with the first possible delivery date which is just the arrival date at the terminal +1.
  • Added a column with an index number.
  • Added a column with a batchgroup (grouping containers per 15)
  • Then i added a working calendar on which days we are open and can receive the 15 containers.

With these steps i tried to calculate the earliest possible delivery date with the following formula:

= let calendar = WorkingCalendar[WorkingDate], eligibleDates = List.Select(calendar, (d) => d >= [FirstPossibleDate]), deliveryDate = if List.Count(eligibleDates) > Number.IntegerDivide([Index], 15) then eligibleDates{Number.IntegerDivide([Index], 15)} else null in deliveryDate

On first sight this seems to work and it groups the containers to a maximum of 15 containers / day.

But when i take a closer look it does not fill the containers to a maximum of 15 / day and sometimes just skips days or start on a new day when the previous day only has 4 / 15 containers planned when there are others that could be booked on the same date.

I don’t seem to find the solution here... I think i might have to do someting with the first possible date changing and it automatically chooses a new delivery date.

(See example of when it changes to 17/06 when it should be 16/06. And only 1 container on 18/06)

I would really appreciate any help on this!!!

7 Upvotes

24 comments sorted by

View all comments

2

u/johndering Jul 08 '25

Can you please share a screenshot with the Index column? Or better yet, with the IntegerDivide of the Index by 15. Thanks.

1

u/Ok_Dimension7436 Jul 08 '25

Sure!

The index just lists all the different containers and they batchgroup (integerdivide) Will make groups of 15.

Clearly my calculation does not work. If you have any solutions that require an entire new Power Pivot I would not mind at all!

Open tot all suggestions 😉

1

u/johndering Jul 08 '25

I think the problem is caused by eligibleDates being recalculated every row using the current FirstPossibleDate. The list index you are using to access members of this "moving" list is relatively "fixed", being the integer of the row Index / 15.

If for example that the eligibleDates is fixed as the Index is also fixed -- just making sure that all FirstPossibleDate values will always be less than the current eligibleDate that will be pointed to by the respective Index -> Batchgroup; then you will always have 15 rows of FirstPossibleDate per eligibleDate.

We cannot use FirstPossibleDate column to count the values it has already used.

An external counter or register of the next available eligibleDate is required -- that counts how many FirstPossibleDate values are using the current value, and looks up the next one, if the count is already 15.

Just my $0.02.

1

u/Ok_Dimension7436 Jul 09 '25

Thank you for sharing your toughts on the IntegerDivide, much appriciated!

I understand what you mean by the fixed grouping of 15 as this doesnt 'move' now.

However, i dont have any idea how to fox this or make sure the maximum of 15 is not surpassed.

Any suggestions on how to fix this?

1

u/johndering Jul 09 '25 edited Jul 09 '25

We can try one thing first, replace the original code:

= let calendar = WorkingCalendar[WorkingDate], eligibleDates = List.Select(calendar, (d) => d >= [FirstPossibleDate]), deliveryDate = if List.Count(eligibleDates) > Number.IntegerDivide([Index], 15) then eligibleDates{Number.IntegerDivide([Index], 15)} else null in deliveryDate

With:

= let calendar = WorkingCalendar[WorkingDate], eligibleDates = calendar, deliveryDate = if List.Count(eligibleDates) > Number.IntegerDivide([Index], 15) and [FirstPossibleDate] <= eligibleDates{Number.IntegerDivide([Index], 15)} then eligibleDates{Number.IntegerDivide([Index], 15)} else null in deliveryDate

If this is good, then you have a better starting point, in case there are edge cases :)

1

u/Ok_Dimension7436 Jul 09 '25

Hi John

Many thanks on your support on this, much appreciated!

Unfortunately the new function returns 'null' on all cases.Which is stranges as there are multiple possible dates...

My PowerQuery Knowlage is not good enough to understand what needs fixing atm...

2

u/johndering Jul 09 '25

Try editing a copy of WorkingCalendar[WorkingDate], deleting rows which are older than the current FirstPossibleDate values.

This is what I meant by edge cases :)

1

u/Ok_Dimension7436 Jul 09 '25

Aha!

We are getting somewhere now John 😁

The function seems tot work perfectlly doe exactly the first 100 cells but return 'null' after that.

Ik checked the calendar but there should ben dates available

1

u/Ok_Dimension7436 Jul 09 '25

This is the calendar

1

u/johndering Jul 09 '25

If you can possibly share a clean version of your Excel file with only the ATA column for the first table, and the Calendar table with only the WorkingDate column. Just these 2 tables with a single column each. Perhaps I can help you better.

1

u/Ok_Dimension7436 Jul 09 '25

Hi John

That would be great!

I am not sure how to share it on Reddit? There don't seems to be an option for that

1

u/johndering Jul 09 '25

Some folks used Google Drive to share Excel files, or something similar for file sharing.

→ More replies