Generate round to nearest 2 interval (text) from 2 values

ooptennoort

Board Regular
Joined
Mar 29, 2021
Messages
67
Office Version
  1. 365
Platform
  1. Windows
Dear experts,
I am trying to convert an excel formula into M for PQ but I think it cannot be done. I need to generate an interval (of 2's) from 2 values.

FirstSecondGreat resultIdeal Result
2.5​
12.5​
6-82-4;4-6;6-8;8-10;10-12;12-14
4.2​
13.2​
8-104-6;6-8;8-10;10-12;12-14
8​
1​
4-60-2;2-4;4-6;6-8
0.1​
4.9​
2-40-2;2-4;4-6
116​
117​
116-118116-118
34​
34​
34-3634-36
32.3​
32.3​
32-3432-34
5​
13​
8-104-6;6-8;8-10;10-12;12-14

The 3rd column would be great. (Did not even attempt the 4th column.)
This was my excel formula:
=IF([@Second]=0,MROUND([@First],2)&"-"&(MROUND([@First],2)+2),MROUND(MAX(0,AVERAGE([@First],[@Second])-1),2)&"-"&MROUND((AVERAGE([@First],[@Second])+1),2))

This is what I produced so far (when I ran into errors, esp. because of &):
= Table.AddColumn(Add2Interval, "2Interval", each if [Second]=0 then Number.ToText(RoundingMode.ToEven([First]/2)*2)
else if [Second]=[First] then [First]
else Number.ToText(RoundingMode.ToEven([Second]/2)*2-1)&"-"&Number.ToText(RoundingMode.ToEven([Second]/2)*2+1), type text)

Anyone?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I know nothing about M code, but I was bored so had a go at getting your ideal result from a formula.
Book1
ABC
1FirstSecondResult
22.512.52-4;4-6;6-8;8-10;10-12;12-14
34.213.24-6;6-8;8-10;10-12;12-14
4810-2;2-4;4-6;6-8
50.14.90-2;2-4;4-6
6116117116-118
7343434-36
832.332.332-34
95134-6;6-8;8-10;10-12;12-14
Sheet1
Cell Formulas
RangeFormula
C2:C9C2=LET(f,FLOOR(MIN([@[First]:[Second]]),2),s,SEQUENCE((CEILING(MAX([@[First]:[Second]]),2)-f)/2+AND([@[First]:[Second]]=INT(MAX([@[First]:[Second]]))),,f,2),TEXTJOIN(";",1,s&"-"&s+2))
 
Last edited:
Upvote 0
I know nothing about M code, but I was bored so had a go at getting your ideal result from a formula.
Book1
ABC
1FirstSecondResult
22.512.52-4;4-6;6-8;8-10;10-12;12-14
34.213.24-6;6-8;8-10;10-12;12-14
4810-2;2-4;4-6;6-8
50.14.90-2;2-4;4-6
6116117116-118
7343434-36
832.332.332-34
95134-6;6-8;8-10;10-12;12-14
Sheet1
Cell Formulas
RangeFormula
C2:C9C2=LET(f,FLOOR(MIN([@[First]:[Second]]),2),s,SEQUENCE((CEILING(MAX([@[First]:[Second]]),2)-f)/2+AND([@[First]:[Second]]=INT(MAX([@[First]:[Second]]))),,f,2),TEXTJOIN(";",1,s&"-"&s+2))

I know nothing about M code, but I was bored so had a go at getting your ideal result from a formula.
Book1
ABC
1FirstSecondResult
22.512.52-4;4-6;6-8;8-10;10-12;12-14
34.213.24-6;6-8;8-10;10-12;12-14
4810-2;2-4;4-6;6-8
50.14.90-2;2-4;4-6
6116117116-118
7343434-36
832.332.332-34
95134-6;6-8;8-10;10-12;12-14
Sheet1
Cell Formulas
RangeFormula
C2:C9C2=LET(f,FLOOR(MIN([@[First]:[Second]]),2),s,SEQUENCE((CEILING(MAX([@[First]:[Second]]),2)-f)/2+AND([@[First]:[Second]]=INT(MAX([@[First]:[Second]]))),,f,2),TEXTJOIN(";",1,s&"-"&s+2))
Dear jasonb75,
Hats off for this... well... magic. I'll give it a try tonight.
That said, should anyone want to try achieving the same in PQ (M)... The drawback of VBA is that running this macro is an extra step after simply refreshing with PQ. And many of my co-workers don't recognise *.xlsm or how to run a macro. And then there's the security issue of VBA...
So hopefully Power Query is an option too ;)
 
Upvote 0
Dear jasonb75,
Hats off for this... well... magic. I'll give it a try tonight.
That said, should anyone want to try achieving the same in PQ (M)... The drawback of VBA is that running this macro is an extra step after simply refreshing with PQ. And many of my co-workers don't recognise *.xlsm or how to run a macro. And then there's the security issue of VBA...
So hopefully Power Query is an option too ;)
I don't see how to implement your function. I tried in VBA, in excel and in power query, but nothing seems to work. How do I do it?
 
Upvote 0
I wonder if "Great results" is your final expection?
Book1
ABCD
1FirstSecondResult1Result2
22.512.56-8
34.213.28-10
4814-6
50.14.92-4
6116117116-118
7343434-34
832.332.332-34
95138-10
Sheet1
Cell Formulas
RangeFormula
C2:C9C2=FLOOR(AVERAGE(A2,B2),2)&"-"&CEILING(AVERAGE(A2,B2),2)


If you still need "Ideal Results", I will try with VBA later.
 
Upvote 0
In the worksheet, it's an excel formula the same as your
This was my excel formula:
=IF([@Second]=0,MROUND([@First],2)&"-"&(MROUND([@First],2)+2),MROUND(MAX(0,AVERAGE([@First],[@Second])-1),2)&"-"&MROUND((AVERAGE([@First],[@Second])+1),2))
 
Upvote 0
In the worksheet, it's an excel formula the same as your
That's what I thought, but I get this error. What am I doing wrong?
 

Attachments

  • convert 2Interbal to M Screenshot 2021-12-03 100914.png
    convert 2Interbal to M Screenshot 2021-12-03 100914.png
    56.4 KB · Views: 6
Upvote 0
I wonder if "Great results" is your final expection?
Book1
ABCD
1FirstSecondResult1Result2
22.512.56-8
34.213.28-10
4814-6
50.14.92-4
6116117116-118
7343434-34
832.332.332-34
95138-10
Sheet1
Cell Formulas
RangeFormula
C2:C9C2=FLOOR(AVERAGE(A2,B2),2)&"-"&CEILING(AVERAGE(A2,B2),2)


If you still need "Ideal Results", I will try with VBA later.
My "need" is for my above excel formula to work in Power Query. This formula gives "good results" (in excel, so far only). Getting PQ to give comma seperated (or whatever) results in one cell would be Ideal (but extremely challenging) ;)
 
Upvote 0
Try this instead, I assume that your data is still formatted as a table, if it is not then it won't recognise the column names as valid references. I have seen before where table names don't copy correctly from the forum but it's usually only when there are spaces in the name and it gets split over 2 lines.
Excel Formula:
=LET(f,FLOOR(MIN([@[First]],[@[Second]]),2),s,SEQUENCE((CEILING(MAX([@[First]],[@[Second]]),2)-f)/2+AND([@[First]]=INT(MAX([@[First]],[@[Second]])),[@[Second]]=INT(MAX([@[First]],[@[Second]]))),,f,2),TEXTJOIN(";",1,s&"-"&s+2))

If that still fails, try changing [@[First]] to A2 and [@[Second]] to B2 then it should accept it but I don't see any reason why the first one has given the error.
 
Upvote 0
=LET(f,FLOOR(MIN([@[First]],[@[Second]]),2),s,SEQUENCE((CEILING(MAX([@[First]],[@[Second]]),2)-f)/2+AND([@[First]]=INT(MAX([@[First]],[@[Second]])),[@[Second]]=INT(MAX([@[First]],[@[Second]]))),,f,2),TEXTJOIN(";",1,s&"-"&s+2))
Wow! (And now I see where I went wrong: indeed, tested on copy of data+formula from form, which isn't in table format.)
This is amazing! It's not in M but it is a semi-solution in that I can have PQ add this formula as text in a column and load it to excel and then click it to turn it into a formula. (Still less clicking than looking up the formula, copying it, pasting it into the cells and filling down).
Mind if I wait if someone can/will address the PQ challenge (before I mark as solution)?!
 
Upvote 0

Forum statistics

Threads
1,223,691
Messages
6,173,851
Members
452,535
Latest member
berdex

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top