web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / How to create a variab...
Power Apps
Unanswered

How to create a variable from a result

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi, i am making an app that count hours and i need to create a variable from a formula result.
This is the function, i tried to save with Set but is not working

If(
    dp_Servicio.Selected.Value = "Trabajos Programados";
    With(
        {
            wStart: dp_Fecha_Inicio_Solicitud.SelectedDate + Time(
                Value(dp_Hora_Inicio.Selected.Value);
                Value(dp_Min_Inicio.Selected.Value);
                0
            );
            wEnd: dp_Fecha_Fin_Solicitud.SelectedDate + Time(
                Value(dp_Hora_Final.Selected.Value);
                Value(dp_Min_Final.Selected.Value);
                0
            )
        };
        With(
            {
                wDiff: wEnd - wStart
            };
            Round(Abs(wDiff) * 24; 1)
        )
    );
//Se encarga de restar 3 horas si el trabajador estaba en turno    
If(
        dp_Turno.Selected.Value = "SI";
        With(
            {
                wStart: dp_Fecha_Inicio_Solicitud.SelectedDate + Time(
                    Value(dp_Hora_Inicio.Selected.Value);
                    Value(dp_Min_Inicio.Selected.Value);
                    0
                );
                wEnd: dp_Fecha_Fin_Solicitud.SelectedDate + Time(
                    Value(dp_Hora_Final.Selected.Value);
                    Value(dp_Min_Final.Selected.Value);
                    0
                )
            };
            With(
                {
                    wDiff: wEnd - wStart
                };
                Round(Abs(wDiff) * 24 - 3; 1)
            )
        );
        With(
            {
                wStart: dp_Fecha_Inicio_Solicitud.SelectedDate + Time(
                    Value(dp_Hora_Inicio.Selected.Value);
                    Value(dp_Min_Inicio.Selected.Value);
                    0
                );
                wEnd: dp_Fecha_Fin_Solicitud.SelectedDate + Time(
                    Value(dp_Hora_Final.Selected.Value);
                    Value(dp_Min_Final.Selected.Value);
                    0
                )
            };
            With(
                {
                    wDiff: wEnd - wStart
                };
                Round(Abs(wDiff) * 24; 1)
            )
        )
    )
)

 

Categories:
I have the same question (0)
  • WiZey Profile Picture
    3,023 Moderator on at

    Hello @Anonymous ,

     

    What have you attempted so far? What kind of error do you get?

     

    Have you tried placing the "Set()" next to the "Round()" functions?

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    i tried putting set in the beginning of the function, and now i tried as u said but i get the same issue

     

    "There is a behavior function in a non-behavior function" powerapps is returning this

     

  • WiZey Profile Picture
    3,023 Moderator on at

    Just like the error said, "Set()" is a behavior function. Meaning it can only be called with a "OnSelect" of a button or other similar properties. 

     

    I believe you've written your formula in a "Text" property, which indeed can't use behavior functions.

     

    Try to put your formula in the "OnSelect" of a button, and set the "Text" property of your control to the value of your variable. 

  • Spamrancher Profile Picture
    345 on at

    It appears that the first and Else of the second If statement are the same. It's really the "SI" that is different. I'm not sure exactly the controls you're using, so you may have to mess with this a bit to get everything to work. I put this formula in the OnChange of dp_Servicio.Selected

     

    Also, the error message you're getting means you're trying to perform an action (create a variable) in a control or property that doesn't allow that. For example, you can't create a variable in the Color property.

     

     

    Set(
     gResult,
     DateDiff(
     dp_Fecha_Inicio_Solicitud.SelectedDate + Time(
     Value(dp_Hora_Inicio.Selected.Value),
     Value(dp_Min_Inicio.Selected.Value),
     0
     ),
     dp_Fecha_Fin_Solicitud.SelectedDate + Time(
     Value(dp_Hora_Final.Selected.Value),
     Value(dp_Min_Final.Selected.Value),
     0
     ),
     TimeUnit.Hours
     ) - If(
     Self.SelectedText.Text = "SI",
     3,
     0
     )
    )

     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 377 Most Valuable Professional

#2
11manish Profile Picture

11manish 165 Super User 2026 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 110 Super User 2026 Season 2

Last 30 days Overall leaderboard