this final code work fine for me.
i pre calculate the "M" value.
// CALCUL DU CUMUL POUR UNE LIGNE
// version 1.0 2023-03-16
// variables utilisateur
var CAS = 0 ;//mettre 0 pour point de début et -1 point de fin
console("cas choisis : "+ CAS )
var SearchingLayer = FeatureSetByName($map,"ROUTE");
var TOLERANCE = 20 ;// la variable TOLERANCE de capture fixée à 5 mètres peut être ajustée si nécessaire
console("tolérance choisie : "+TOLERANCE)
// 1 - Extraction du point de calcul et recherche
var myPoint = Point({x: Geometry($feature)['paths'][CAS][CAS]['x'],y: Geometry($feature)['paths'][CAS][CAS]['y'],spatialReference: { wkid: 102100 }})
console("coordonnées de mon point : "+ myPoint)
var buff = Extent(bufferGeodetic(myPoint,TOLERANCE,'meters'));
var myGeom = myPoint;
var MySearch = Intersects(BufferGeodetic(myGeom, TOLERANCE, "meters"),SearchingLayer);
// le calcul renvoit -1 pour signifier absence de rd à proximité
if(isempty(Mysearch)){return -1}
// dans les autres cas on calcul pour le cas choisi.
if(count(Mysearch)==1){
console("cas une seule rd")
var Arc = first(Mysearch)
}
else{
console("cas plusieurs rd")
var EvDistance = Infinity;
var MyNearest;
for (var listing in MySearch){
var DynDistance = Distance(listing,myGeom, "meters");
if(DynDistance < EvDistance){
EvDistance = DynDistance;
MyNearest = listing;
var Arc = MyNearest
}
}
}
var longueur = 0;
var p = 0;
var newLine = Dictionary(text(Geometry(Arc)));
var reste = Round(LengthGeodetic(First(Cut(Arc,Rotate(clip(Arc,buff),90)))));
var cutter = Rotate(clip(Arc,buff),90);
for (p in newLine.paths){
var segment = Polyline({paths: [newLine.paths[p]],spatialReference:{latestWkid:3857,wkid:102100}})
var segLongueur = lengthGeodetic(segment,'meters')
if (intersects(segment,cutter)==true)
{return round(longueur+reste)}
else
{longueur += segLongueur;}