Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Nacho_P
Regular Visitor

Random Values for each row by calling a custom function

Hi everyone

It´s my 1st time asking for help to the community... Hoping for the best!  

 

I need to define, for each row in a table, a random time between 4 defined parameters.

 

That random time is being calculated by calling, for each row,  a custom Function (RandomTime), which works with those set parameters:

 

 

Custom3 = Table.AddColumn(Custom2, "Hora Corte Reparto AJ",
each
RandomTime(#"1er Slot Preparacion RNA",#"2do Slot Preparacion RNA",#"3er Slot Preparacion RNA",#"4to Slot Preparacion RNA")),

 

 

The calculation is being done correctly, but the problem is that the result for each row is the same, and I do not know where the problem lies....

Any ideas on how to solve this?

 

Much appreciated!

 

The custom functions are the following =>

 

(A,B,C,D as time) =>

let
RandomA = Number.From(A),
RandomB = Number.From(B),
RandomC= Number.From(C),
RandomD= Number.From(D),

Random1 =Number.RandomBetween(RandomA,RandomB),

Random2 =Number.RandomBetween(RandomC,RandomD),

ResultadoRandom=Number.RandomBetween(Random1,Random2),

Delta1=Number.Abs(Random1-ResultadoRandom),

Delta2= Number.Abs(Random2-ResultadoRandom),

RandomFinal= if Delta1<=Delta2
then Random1
else Random2,
RandomFinal2 = RoundownTime(Time.From(RandomFinal),TimeSlot)

in
RandomFinal2

 

 

 

(TimeToRound as time,TimeSlot as number ) =>      
let

    Source = TimeToRound,
    #"Convert datetime to decimal" = Number.From(Source),

    #"Upscale number" = #"Convert datetime to decimal"*(24*60/TimeSlot),

    #"Round to nearest minute" = Number.Round(#"Upscale number"),

    #"Downscale number" = #"Round to nearest minute" / (24*60/TimeSlot),

    #"Convert back to time" = Time.From(#"Downscale number")
    
in
    #"Convert back to time"

 

 

 

 

 

 

3 REPLIES 3
Nacho_P
Regular Visitor

Just to showcase the result I'm getting rigth now..

Nacho_P_0-1652975043449.png

 

Hi! @Nacho_P ,

 

Can you please share the sample data and desired output that you need?

 

For Power BI trainings or support dm or reach out to me on LinkedIn.
If my response has successfully addressed your question or concern, I kindly request that you mark this post as resolved. Additionally, if you found my assistance helpful, a thumbs-up would be greatly appreciated.

Thanks,
Ankit Kukreja
www.linkedin.com/in/ankit-kukreja1904
https://topmate.io/ankit_kukreja/

Hi @AnkitKukreja , thank you for your reply.
The desired outcome is, for each row, a different randomized time based on the parameters which the custom function computes.

The problem I'm having is that, although I'm using an each statement, the random number is not generated for each row, but instead becomes fixed for the whole iteration.
The custom function requires 4 time parameters, and the result should be a random time that falls between A-B or between B-C.
Those time parameters currently are:
A)8:00
B)11:40
C)14:20
D)19:40

Hope this adds clarity to my question.

 

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors